devdar
devdar

Reputation: 5654

Accessing User Profile on Graph Microsoft using OKHttp returns special characters

I am trying to retrieve my user profile from graph.microsoft as show here. I am using a Java library OKHttp to achieve this however the server is returning special characters in the response. I checked my headers and I did include "Accept-Encoding: gzip". However the issue is not resolved. See code under;

Java Code

Request userProfileRequest = new Request.Builder()
     .url("https://graph.microsoft.com/v1.0/me")
     .get()
     .addHeader("Authorization", "Bearer "+accessTkn)
     .addHeader("Accept", "*/*")
     .addHeader("Cache-Control", "no-cache")
     .addHeader("Content-Type", "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8")
     .addHeader("Accept-Encoding", "gzip")
     .addHeader("Connection", "keep-alive")                                 
     .addHeader("cache-control", "no-cache")
     .build();

Response userProfileResponse = client2.newCall(userProfileRequest).execute();           
System.out.println("Authorization is  " +userProfileRequest.header("Authorization"));           
System.out.println(userProfileResponse.body().string());

Console output enter image description here

Upvotes: 1

Views: 143

Answers (2)

Yuri Schimke
Yuri Schimke

Reputation: 13478

OkHttp does transparent compression for you. However by explicitly specifying "Accept-Encoding: gzip" you are indicating that you want gzip compression and will handle it yourself.

Removing everything except Authorization as you have done in your answer is the correct solution.

Upvotes: 2

devdar
devdar

Reputation: 5654

The solution that worked for me was to remove all the headers except for "Authorization"

Java Code

Request userProfileRequest = new Request.Builder()
 .url("https://graph.microsoft.com/v1.0/me")
 .get()
 .addHeader("Authorization", "Bearer "+accessTkn)                                       
 .build();

Upvotes: 0

Related Questions