GET Okhttp Java Request

Asked

Viewed 658 times

3

I’m using Postman, and when making a requisition GET, everything goes well and the JSON with the data.However, when I take the java code(below), this request and execute it, I am handed the following message code=406, message=Not Acceptable, which is strange since it works well in Postman.Someone knows what it is?

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
  .url("http://api.sualoja.com.br/clientes")
  .get()
  .addHeader("authorization", "Bearer d0f36dfb6ba2d153a29f4410411f737e3a6e205c")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "0465578e-2915-c482-d425-8c00ee97ec6d")
  .build();

Response response = client.newCall(request).execute();

I tried to use another way to request using Unirest but code below but returned me the msm error, but with this message more:Unable to resolve Accept header to a representation, remembering that I took this code from Postman too, and that there it works smoothly.

HttpResponse<String> response = Unirest.get("http://api.sualoja.com.br/clientes")
            .header("authorization", "Bearer 256ce87661709544aa32e6e97f779e0e5d9aa842")
            .header("cache-control", "no-cache")
            .header("postman-token", "23b4718d-3831-cf52-dc0a-b07a3f8caa1e")
            .asString();

I used the method to show the headers, and are returned these headers Transfer-Encoding=[chunked], Server=[nginx/1.10.2], Connection=[keep-alive], Date=[Tue, 03 Oct 2017 20:05:16 GMT], Content-Type=[application/problem+json], X-Powered-By=[PHP/7.1.10]

I tested with the code represented in shell that Postman allows me to take, and it worked well, brought all the data in the terminal, but I need to do this in java.

PEOPLE I finally managed to resolve. I simply added this line to my GET request .addHeader("Accept", "application/json; q=0.5"), and from what I understand, Voce will have to add a new line of these for each type of file that your server returns, for example:

`.addHeader("Accept", "application/hal+json; q=0.5")
 .addHeader("Accept", "application/json; q=0.5")
 .addHeader("Accept", "application/x-www-form-urlencoded")`

And my code went like this. (Using Okhttp):

OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("http://api.sualoja.com.br/clientes")
            .get()
            .addHeader("authorization", "Bearer 1c2b5f30582b4cf2831812487a34d48646580829")
            .addHeader("cache-control", "no-cache")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("postman-token", "57989a0f-8dec-d104-c6e7-b6e2cb64be97")
            .build();

    Response response = client.newCall(request).execute();
    System.out.println(response.body().string());
  • I think you’re missing the content type in your header

  • like this .addHeader("content-type", "json") and continued the msm error, but in Postman continued to work

  • trial .addHeader("content-type", "application/json")

  • still persists in error

  • 1

    at Postman, you can see which headers he’s sending, it’s the same ?

  • Yes, Postman is only sending this Authorization:Bearer 922da365f641a4bb2ba9d06d9d5ca88233d26260. I removed the others and only left the authorizationand it didn’t work either

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.