Consuming weird content-types with Micronaut HttpClient
I just lost 1 hour searching, so I am just blogging to raise awareness and provide an extra link to the existing documentation of Micronaut, which I managed to miss, just before my eyes.
So I was trying to consume an API (HTTP/JSON) with response text/javascript
1 Content-Type: "text/javascript"
I had totally missed that specific case, and I was wondering why this piece of code that I had written several times was not working. The httpclient snippet looks like this.
1public Flowable<CustomResponseWrapper> findRecipe(String qName){
2 var url= UriBuilder.of(BASE_URL).queryParam("q",qName).build();
3 HttpRequest request=HttpRequest.GET(url);
4 return httpClient.retrieve(request,CustomResponseWrapper.class);
5}
Obviously when you try to test it you get several errors, things like empty_body, etc.
1[
2 {
3 "timestamp": "2020-12-26T17:13:49.246Z",
4 "level": "TRACE",
5 "logger_name": "io.micronaut.http.client.netty.DefaultHttpClient",
6 "message": "Unable to convert response body to target type class com.javapapo.xxxx.CustomWrapperResponse.class"
7 },
8 {
9 "timestamp": "2020-12-26T17:13:49.248Z",
10 "level": "ERROR",
11 "logger_name": "io.micronaut.http.netty.stream.HttpStreamsHandler",
12 "message": "Error occurred writing stream response: Failed to decode the body for the given content type [text/javascript]",
13 "stack_trace": "io.micronaut.http.client.exceptions.HttpClientResponseException: Failed to decode the body for the given content type [text/javascript]"
14 }
15]
I found the solution on this github issue but it proved there was already a section on the official documentation as well. See here
The fix was just 1 line of extra config as per documentation
1micronaut.codec.json.additionalTypes: [ "text/javascript" ]