1
votes

I am trying to make a patch rest request to another API that accepts content type "application/json-patch+json". I am using Spring's webclient but I have not been able to get it to work. I keep getting "415 Unsupported media type"

I have tried the below;

WebClient webClient = WebClient.create(baseUrl);
Mono<ClientResponse> response = webClient.patch()
  .uri(updateVmfExecutionApi, uuid)
  .header("Content-Type", "application/json-patch+json")
  .body(BodyInserters.fromFormData("lastKnownState", state))
  .exchange();

I also tried:

WebClient webClient = WebClient.create(baseUrl);
    Mono<ClientResponse> response = webClient.patch()
      .uri(updateVmfExecutionApi, uuid)
      .contentType(MediaType.valueOf("application/json-patch+json"))
      .body(BodyInserters.fromFormData("lastKnownState", state))
      .exchange();

For both cases I see the following error;

 {"timestamp":"2020-09-17T20:50:40.818+0000","status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Unsupported Media Type","trace":"org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:215)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)\n\tat org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:449)

Seems it changes to 'application/x-www-form-urlencoded;charset=UTF-8' Is it even possible to use webclient for this content-type?

1
Are you sure your API accepts that Content-Type? Have you tried testing it outside of your application, i.e. Postman?George
Yes I am sure it accepts that content type. I tried testing with the swagger ui and it works. Side note: Its not my API, actually trying to call the API for another servicerand
it changes because of the body you are sending, you are declaring that the body is going to contain json and then you stick form data in the body, try changing to .bodyValue(state)Toerktumlare
@ThomasAndolf that solved the problem. Thanks a lotrand

1 Answers

3
votes

If you look into the exception you can see it says

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

It changed it to formdata. Thats because what you actually send in the body has priority. In your code you are stating the following to send the body.

.body(BodyInserters.fromFormData("lastKnownState", state))

This states that you are sending form data, which is a key value way of sending data, and webclient will then set the content type header for you automatically to x-www-form-urlencoded.

You need to send json data if you want to have a json content type header. Sending json is the default way for webclient so all you need to do is to pass in the body correctly. There are several ways of passing the body in the standard way.

either by passing a producer (could be a Mono or a Flux).

.body(Mono.just(data))

Using BodyInserter#fromValue.

.body(BodyInserters.fromValue(data))

or the shorthand for the previous one (which is the simplest)

.bodyValue(data)