0
votes

I need to save the PDF of the POST service, but I always receive the error:

"Error while extracting response for type [class java.lang.Object] and content type [application/pdf]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"

I am using Spring Boot and RestTemplate

Example:

public static final String base_url = "https://evx.jsreportonline.net/api/report";

static {
    HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("evx.jsreportonline.net"));
}

private HttpHeaders getHeaders() {
    if (null == headers) {
        headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.ALL));
        headers.add("Authorization", "Basic " + Base64.getEncoder().encodeToString("test_pdf:test_pdf".getBytes()));
    }
    return headers;
}

public void getRepo() {

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    RestTemplate restTemplate = new RestTemplate();
    Json jsonObj = new Json("{\"template\":{\"shortid\":\"JCdUaT_\"},\"data\":null}");

    // REQUEST
    HttpEntity<Object> entity = new HttpEntity<>(jsonObj, this.getHeaders());

    /* MENSAGE CONVERTER */
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
    messageConverters.add(converter);
    restTemplate.setMessageConverters(messageConverters);

    /* RESPONSE */
    try {
        Object result = restTemplate.postForObject(base_url, entity, Object.class);
        byte[] resultByteArr = (byte[]) result;
    } catch (Exception e) {
        System.out.println(e.getMessage() + " " + e);
    }
}
2

2 Answers

0
votes

In situations like this curl can be really useful for debugging.

If I use your attributes, auth, json payload and api endpoint and post the request with curl

curl -i --user test_pdf:test_pdf -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"template":{"shortid":"JCdUaT_"},"data":null}' https://evx.jsreportonline.net/api/report

I get the following repsonse

HTTP/2 200 
date: Fri, 24 Jan 2020 19:45:07 GMT
content-type: application/pdf
server: nginx/1.17.6
x-powered-by: Express
access-control-allow-origin: *
access-control-expose-headers: *
set-cookie: render-complete=true; Path=/
set-cookie:  
content-disposition: inline;filename=invoice-main.pdf
jo-credits-spent: 1.12

Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.

This is telling me that I am getting binary data as a response. In that case I would need to use a different converter. Try the 'ByteArrayHttpMessageConverter' as shown here good ans

0
votes

I solved it. Likewise, thank you very much to those who responded.

public static final String base_url = "https://evx.jsreportonline.net/api/report";


static {
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("evx.jsreportonline.net"));

}

private HttpHeaders getHeaders() {
if (null == headers) {
    headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.ALL));
    headers.add("Authorization", "Basic " + Base64.getEncoder().encodeToString("test_pdf:test_pdf".getBytes()));
}
return headers;

}

public void getRepo() {

Json reporte = new Json("{\"template\":{\"shortid\":\"ytyg1Ho\"},\"data\":null}");

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

/* MENSAGE CONVERTER */
messageConverters.add(new ByteArrayHttpMessageConverter());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);

RestTemplate restTemplate = new RestTemplate(messageConverters);

 // REQUEST
HttpEntity<Object> entity = new HttpEntity<>(reporte, this.getHeaders());

   /* RESPONSE */
try {
    byte[] response = restTemplate.postForObject(base_url, entity, byte[].class);
    System.out.println("Response "+new String(response));
}catch (Exception e) {
    System.out.println(e.getMessage() +" "+ e);
}

}