Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding of response http response body

I'm using okhttp library for sending request to rest api. this is my java code for sending request to https:

RequestBody body = RequestBody.create(JSON, requestBody);

    Request request = new Request.Builder().url("https://examplesite.com/json/").post(body)
            .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
            .addHeader("Accept-Encoding", "gzip").addHeader("Accept-Language", "en-US,en;q=0.8,fa;q=0.6,ar;q=0.4")
            .build();

    Response response = client.newCall(request).execute();
    String res = new String(response.body().string().getBytes("UTF-8"));

    System.out.println(res);

the res variable is: �CU8{$���'L�@R�W*�$��b�H�E�l�K�C� 30��}c&,p��q���)+3�R�28���#SC�

what is the encoding of above text?

this is response header:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,fa;q=0.6,ar;q=0.4
Connection:keep-alive
Content-Length:95
Content-Type:application/json

I can't understand what is the encoding of the response body. whatever when I send request by postman extension on chrome that response body is a normal json. attend that the protocol is https and I think okhttp library handle encrypting and decripting data.

like image 671
mahdi Avatar asked Jan 06 '23 09:01

mahdi


1 Answers

Remove this:

 .addHeader("Accept-Encoding", "gzip")

When you do this, you’re telling OkHttp that you want to manage your own response compression.

If you don’t explicitly configure the Accept-Encoding, OkHttp will take care of you. It’ll add the header to the request, and decompress the response.

like image 92
Jesse Wilson Avatar answered Jan 09 '23 23:01

Jesse Wilson