简体   繁体   中英

Invalid mime type "text/plain charset=UTF-8": Invalid token character ' ' in token "plain charset=UTF-8"

I'm trying to send a GET request to a third party API that is using a CloudFlare.

This was working before but suddenly just this few days without changing from my code this exception kept on throwing.

org.springframework.http.InvalidMediaTypeException: Invalid mime type "text/plain charset=UTF-8": Invalid token character ' ' in token "plain charset=UTF-8"
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:926)
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:133)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:995)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:978)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)
    at com.baxin.sscapi.cpdatacollector.fetch.FetchService$Callable_Fetch.call(FetchService.java:469)
    at com.baxin.sscapi.cpdatacollector.fetch.FetchService$Callable_Fetch.call(FetchService.java:438)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "text/plain charset=UTF-8": Invalid token character ' ' in token "plain charset=UTF-8"
    at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:247)
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:531)
    ... 16 common frames omitted

Here is my code. Please note that this was working before.

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setBearerAuth(token);
headers.set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
HttpEntity request = new HttpEntity(headers);

responseData = restTemplate.exchange(url, HttpMethod.GET, request, String.class,1);

Also, please note that this request is a multithreaded and being executed so often with milliseconds intervals.

I'm really looking forward for any suggestions or opinions on this.

Thank you.

Since the error comes from that the content-type is missing a ; after the media type in the response, it is most likely due to a change by the third party API (which is also different from the media-type you request).

A well-formatted content-type would be text/plain; charset=UTF-8 text/plain; charset=UTF-8

I suggest to contact the API provider.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM