Search code examples
javaandroidhttp-request

Get Response Code and Response Body ina a single request using Http-request


I am using the library http-request, from https://github.com/kevinsawicki/http-request

I need to make a GET request to server and get response code and response body separately, given a request.

How can I do that using this lib?

There is only examples retrieving one of them, like :

String response = HttpRequest.get("http://google.com").body();
System.out.println("Response was: " + response);

and

int response = HttpRequest.get("http://google.com").code();

Solution

  • Store the HttpRequest object in a variable

    HttpRequest request = HttpRequest.get("http://google.com");
    

    Invoke what you need

    request.body();
    request.code();
    

    (It's weird to get the response body on a request object...I don't like this library.)