Request GET OkHttp Java

3

I'm using Postman , and when I make a request GET , everything happens in a good way and returns the JSON with the data. But by taking the code in java (below), of that request and executing it, I get the following message code=406, message=Not Acceptable , which is strange since it works good on Postman. Anyone know 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 form to make a request using Unirest , but it returned the msm error, but with that message: Unable to resolve Accept header to a representation , remembering that I got this code from Postman too, and that it works there .

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 these headers are returned 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 the shell that Postman allows me to get, and it worked fine, I brought all the data in the terminal, but I need to do it in java.

I finally got it resolved. I simply added this line to my GET .addHeader("Accept", "application/json; q=0.5") request, and from what I understand, you will have to create 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")'

Aah and my code stayed 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());
    
asked by anonymous 02.10.2017 / 22:29

0 answers