0

This is the full error message from my app logs

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL -CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens

I am sending a POST request to my web app using postman with the following header The inputs is a gzip with json in it. On postman, it returns 400 Bad Request

 Content-Type: application/json
 Content-Encoding: gzip

This is how I upload my gzip file

This is my POST method. I have a custom gzip interceptor class.

@RequestMapping(
   method = RequestMethod.POST,
   value = "/post"
)
public WekaPredictResp prediction(HttpServletRequest request, HttpServletResponse response, @RequestBody CustomRequest gzipInputs) throws Exception {
   postMethod(gzipInputs);
}
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;

public static CustomResponse postMethod(CustomRequest gzipInputs) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(gzipInputs.getInputs(), headers);

    RestTemplate gzipRestTemplate = new RestTemplateBuilder()
       .requestFactory(HttpComponentsClientHttpRequestFactory.class)
       .additionalInterceptors(new GzipInterceptor())
       .build();

    ResponseEntity<Map> responseEntity = gzipRestTemplate.postForEntity(externalUrl, entity, Map.class);

    Map<String, Object> outputs = (Map<String, Object>) responseEntity.getBody();

    CustomResponse response = new CustomResponse();
    response.setOutputs(outputs);

    return outputs;

}
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class GzipHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        HttpHeaders httpHeaders = httpRequest.getHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "gzip");
        httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
        httpHeaders.set(HttpHeaders.CONTENT_TYPE, "gzip"); // Override for compatibility
        return clientHttpRequestExecution.execute(httpRequest, compress(bytes));
    }

}
Chia Yi
  • 562
  • 2
  • 7
  • 21

1 Answers1

0

by including a filter to track gzip inputs and decompress it, sample solution can be found here

Chia Yi
  • 562
  • 2
  • 7
  • 21