Raja Asthana
Raja Asthana

Reputation: 2100

Spring - RestTemplate throwing InvalidMediaTypeException

I'm working on Spring RestTemplate and i'm getting InvalidMediaTypeException after the execution of the below code. When I execute the same service in RestClient app, i'm getting a valid response. Kindly help.

ResponseEntity<String> response = restTemplate.exchange(restUrl,HttpMethod.valueOf(method), new HttpEntity<byte[]>(headers), String.class);

Below is the stacktrace.

org.springframework.http.InvalidMediaTypeException: Invalid media type "multipart/mixed;boundary=simple boundary;charset=UTF-8": Invalid token character ' ' in token "simple boundary"
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:730)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:305)
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:113)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:687)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:673)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:446)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:393)
    at com.restclient.helper.RestHelper.getResponse(RestHelper.java:28)

Upvotes: 2

Views: 7787

Answers (3)

kuporific
kuporific

Reputation: 10323

The exception and stack trace says it all:

On the client-side you have:

ResponseEntity<String> response 
    = restTemplate.exchange(
        restUrl,
        HttpMethod.valueOf(method), 
        new HttpEntity<byte[]>(headers), // <-- contains bad "Content-Type" value
        String.class);

The headers map contains

"Content-Type" -> "multipart/mixed;boundary=simple boundary;charset=UTF-8"`

When the request hits the server, it tries to parse this header value with MediaType#parseMediaType(String), but the space character is invalid, as noted by the exception message:

Invalid token character ' ' in token "simple boundary"

Next step is to investigate how headers is populated.

Upvotes: 0

holmis83
holmis83

Reputation: 16604

Spring parses the Content-Type of the response quite strict. As the error message implies, space character is not allowed in the Content-Type field (unless it is quoted). You can read about it in RFC 2616 section 2.2 or RFC 2045 section 5.1. Make sure the server you are calling comply to these rules.

Upvotes: 0

Prototype
Prototype

Reputation: 11

This because missmatch between client content type and server accept content type. Bassically normal "GET" method the default content type is "text/plain" but is ur case server require something not "text/plain". So u should change contenttype of header when u send request to your server

Upvotes: 1

Related Questions