1

I’m having issue consuming REST API. Below is my rest client. It fails at the service call with error:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value
at [Source: java.io.PushbackInputStream@62a8dd06; line: 11, column: 55]

When I use the same json string (printed in the log) in postman, it works. It fails when I fire the request from my client.

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + base64Creds);
try {
    request = mapper.constructRequest(txnRequest, params);
    logger.debug(method + " Request: " + request);
    ObjectMapper map = new ObjectMapper();
    logger.debug(method + " Request in json format: " + 
    map.writeValueAsString(request));
             
    myRequest = new HttpEntity<MyRequest>(request, headers);
    response = restTemplate.exchange(url, HttpMethod.POST, myRequest, MyResponse.class);
    logger.debug(method + " response: " + response);
} catch(Exception ex) {
    ex.printStackTrace();
}

Any help is much appreciated.

devReddit
  • 2,696
  • 1
  • 5
  • 20
alex
  • 21
  • 1
  • 5
  • 1
    character code 10 (in ascii or almost any other character set) is a newline, so the problem is probably an unexpected line break getting inserted somewhere. – Chris Dodd Aug 11 '21 at 20:55
  • Thanks Chris for your response. I switched to traditional HttpURLConnection to make this work. In this way, I'm able to set the input and output format with utf-8 and it worked. I tried to do the same with rest template, but the issue was still persisting. So, I'd to switch my client implementation to HttpURLConnection. – alex Aug 19 '21 at 06:31

1 Answers1

0

I switched to traditional HttpURLConnection to make this work. In this way, I'm able to set the input and output format with utf-8 and it worked. I tried to do the same with rest template, but the issue was persisting. So, I'd to switch my client implementation to HttpURLConnection

alex
  • 21
  • 1
  • 5