1

I just spent 5 hours trying to solve this issue but I've made zero progress. I've tried all the solutions I could find but I'm stuck.

Here's my basic setup and the issue that I'm facing:

Basic Setup

I have 2 functions -

1) callGetApi(String url)

2) callPostApi(String url, String requestBody)

The code is more or less the same:

callGetApi:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic " + apiAuthorizationString);
ResponseEntity<String> entity = null;
try {

    entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(headers), String.class);
    apiOutput = gson.fromJson(entity.getBody().toString(), ApiOutput.class);
}
....

callPostApi:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic " + apiAuthorizationString);
HttpEntity<Object> request = new HttpEntity<Object>(request_body, headers);
ResponseEntity<String> entity = null;
try {

     entity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
     apiOutput = gson.fromJson(entity.getBody().toString(), ApiOutput.class);
    }
.....

Issue

The issue that I'm facing is that in the callGetApi() function, I'm receiving proper response which is getting decoded properly, but I'm receiving differently-encoded response in the CallPostApi() function which I'm not able to decode. Both of these are working perfectly fine in PostMan with the same input.

Output I'm receiving in callPostApi() function call:

Debugger Output

Output Received in Postman for same input and headers:

{"SuccessData":"Ticket not saved as no changes madeINC024","ErrorData":null,"AppData":null}

In the callGetApi() function I always receive these headers:

{Transfer-Encoding=[chunked], Content-Type=[text/plain; charset=utf-8], Server=[Kestrel], X-Powered-By=[ASP.NET], Date=[Mon, 15 Oct 2018 13:05:27 GMT]}

In the callPostApi() function, I always receive these headers:

 {Transfer-Encoding=[chunked], Content-Type=[text/plain; charset=utf-16], Server=[Kestrel], X-Powered-By=[ASP.NET], Date=[Mon, 15 Oct 2018 13:06:58 GMT]}

And at the same time, the same API endpoint for callPostApi() function is sending the proper headers in Postman(same headers as the one for callGetApi() above)

Solutions I've tried:

I've tried all PnCs of the following:

1) Added StringHttpMessageConverter for UTF-16 and UTF-8 both separately and together:

restTemplate.getMessageConverters().add(0, new  StringHttpMessageConverter(Charset.forName("UTF-16")));

2) Removed all other converters:

restTemplate.getMessageConverters().clear();

3) Added the following headers in callPostApi():

headers.add("Accept-Encoding", "identity");
headers.add("Cache-Control", "no-cache");
headers.add("Accept-Charset", "utf-8");
headers.add("Accept", "application/json, charset=utf-8");
headers.set("Accept-Language", "en");

4) Used HttpHeaders instead of MultiValueMap:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + apiAuthorizationString);
headers.add("Accept-Encoding", "identity");
headers.add("Cache-Control", "no-cache");
headers.add("Accept-Charset", "utf-8");

Something that I tried that showed some progress:

entity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
//entity has the same data as the debugger screenshot I've attached above
byte[] utf8 = entity.getBody().toString().getBytes("UTF-16");
String string = new String(utf8, "UTF-8");

On running the above code, the string array contains the correct output BUT with spaces between all characters:

[{,,",,S,,u,,c,,c,,e,,s,,s,,D,,a,,t,,a,,",,:,,",,T,,i,,c,,k,,e,,t,, ,,n,,o,,t,, ,,s,,a,,v,,e,,d,, ,,a,,s,, ,,n,,o,, ,,c,,h,,a,,n,,g,,e,,s,, ,,m,,a,,d,,e,,I,,N,,C,,0,,2,,4,,",,,,,",,E,,r,,r,,o,,r,,D,,a,,t,,a,,",,:,,n,,u,,l,,l,,,,,",,A,,p,,p,,D,,a,,t,,a,,",,:,,n,,u,,l,,l,,},]

the byte array contains the following data:

[123, 0, 34, 0, 83, 0, 117, 0, 99, 0, 99, 0, 101, 0, 115, 0, 115, 0, 68, 0, 97, 0, 116, 0, 97, 0, 34, 0, 58, 0, 34, 0, 84, 0, 105, 0, 99, 0, 107, 0, 101, 0, 116, 0, 32, 0, 110, 0, 111, 0, 116, 0, 32, 0, 115, 0, 97, 0, 118, 0, 101, 0, 100, 0, 32, 0, 97, 0, 115, 0, 32, 0, 110, 0, 111, 0, 32, 0, 99, 0, 104, 0, 97, 0, 110, 0, 103, 0, 101, 0, 115, 0, 32, 0, 109, 0, 97, 0, 100, 0, 101, 0, 73, 0, 78, 0, 67, 0, 48, 0, 50, 0, 52, 0, 34, 0, 44, 0, 34, 0, 69, 0, 114, 0, 114, 0, 111, 0, 114, 0, 68, 0, 97, 0, 116, 0, 97, 0, 34, 0, 58, 0, 110, 0, 117, 0, 108, 0, 108, 0, 44, 0, 34, 0, 65, 0, 112, 0, 112, 0, 68, 0, 97, 0, 116, 0, 97, 0, 34, 0, 58, 0, 110, 0, 117, 0, 108, 0, 108, 0, 125, 0]

Any idea how I can fix this? All help is appreciated

MGoyal92
  • 31
  • 3
  • `ResponseEntity> entity = restTemplate.exchange(url, HttpMethod.POST, request, Hashmap.class);` – Hadi J Oct 15 '18 at 16:21
  • @HadiJ Thanks! That didn't work, but I tried ResponseEntity entity = restTemplate.exchange(url,HttpMethod.POST, request, Object.class), and that worked. – MGoyal92 Oct 16 '18 at 07:08

1 Answers1

2

Changing ResponseEntity's type from String to Object worked. The code is:

ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, request, Object.class);

Thanks to @HadiJ for pointing me in the right direction.

MGoyal92
  • 31
  • 3