How to Get an Http Response Body as a String

How can I get an HTTP response body as a string?

Every library I can think of returns a stream. You could use IOUtils.toString() from Apache Commons IO to read an InputStream into a String in one method call. E.g.:

URL url = new URL("http://www.example.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);

Update: I changed the example above to use the content encoding from the response if available. Otherwise it'll default to UTF-8 as a best guess, instead of using the local system default.

Access HTTP response as string in Go

Get Json string from http response.body()

I managed to solve my problem by creating a class like this:

public class BlockResponse {

public String er;
}

And then I used the google-Gson to handle everything by doing this:

String serverResponse = response.body().string();
Gson gson = new Gson();
result = gson.fromJson(serverResponse, BlockResponse.class);

And for the comparison I used:

if (result != null && result.er.equals("manualBlock")) {
throw new BlockeduserException("User blocked", null);
}

How to convert the new HTTP Client response body to String in Java 9?

The javadoc correctly states that Implementations are available in HttpResponse

HttpResponse response = HttpRequest
.create(new URI("http://stackoverflow.com/"))
.GET()
.response();

String body = response.body(HttpResponse.asString());

How can I get http response body when request is failed in java?

Try the below code :

package com.abc.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String url = "http://localhost:8888/login?token=token";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
int responseCode = connection.getResponseCode();
InputStream inputStream;
if (200 <= responseCode && responseCode <= 299) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
}

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder response = new StringBuilder();
String currentLine;

while ((currentLine = in.readLine()) != null)
response.append(currentLine);

System.out.println(response.toString());
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Angular HTTP request get response body

You need to use JSON.stringify to see the actual content

 console.log('resp: ' + JSON.stringify(event.body));

if you want to access particular field from the response, use JSON.parse to convert to Object

let filename = (JSON.parse(event.body)).fileName;

Dart can not use a response.body as a String

Found the issue, thanks to the clue in the comments from Ro.

Solved with String.trim();

I guess there must have been a hidden trailing space that I was unaware of?



Related Topics



Leave a reply



Submit