How to get raw JSON text from Unirest response in Java How to get raw JSON text from Unirest response in Java json json

How to get raw JSON text from Unirest response in Java


The Unirest API supports this out of the box - Use asString() instead of asJson() to get the response as HttpResponse<String>.

HttpResponse<String> response = Unirest                .post("http://myserver.com/file")                  .header("cache-control", "no-cache")                  .header("Postman-Token", "02ec2fa1-afdf-4a2a-a535-353424d99400")                .header("Content-Type", "application/json")                .body("{some JSON body}")                .asString();System.out.println(response.getBody());


You can do it like this:

InputStream inputStream = response.getRawBody();BufferedInputStream bis = new BufferedInputStream(inputStream);ByteArrayOutputStream buf = new ByteArrayOutputStream();int result = bis.read();while(result != -1) {    buf.write((byte) result);    result = bis.read();}String rawJson = buf.toString("UTF-8");