PUT Request throws 415 Unsupported Media Type: [no body]

  Kiến thức lập trình

I do not have a lot of experience in this area so please forgive me.

I have a simple PUT request which takes in a request body with an Integer id and a List of ids (passed from the frontend as an array).

{ id: 1, itemIds: [10, 11, 12] }

When the rest API takes this request it looks correct. I have it set up as a void method, and to not send a response body back, just a 200.

The failure happens during this method, restTemplate.exchange. I’m assuming when it hits the httpEntity (modified for posting here):

  public void updateItems(final UpdateRequest updateRequest) {
    UriComponents uriBuilder =
        UriComponentsBuilder
            .fromHttpUrl(baseUrl + UPDATE_PATH)
            .build();
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ALLOW, HttpMethod.PUT.name());
    headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    HttpEntity<UpdateRequest> httpEntity =
        new HttpEntity<>(updateRequest, headers);
  
    try {
      restTemplate
          .exchange(
              uriBuilder.toUriString(),
              HttpMethod.PUT,
              httpEntity,
              new ParameterizedTypeReference<Void>() {});
    } catch (HttpStatusCodeException e) {
      throw handleHttpClientErrorException(e);
    }
  }

I took a quick look at the server logs to see if I could find out where this error is occurring (modified for posting here):

2024-05-15 21:20:26.030 INFO  71833 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=REQUEST|requestMethod=PUT|requestUri=/app/api/my-endpoint|requestHeaders=[[{Accept-Language:en-US,en;q=0.9,Accept-Encoding:gzip, deflate, br, zstd,Referer:http://localhost:9083/app/ui/,Sec-Fetch-Dest:empty,Sec-Fetch-Mode:cors,Sec-Fetch-Site:same-origin,sec-ch-ua-platform:"Windows",User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36,sec-ch-ua-mobile:?0,Content-Type:application/json,Accept:application/json, text/plain, */*,sec-ch-ua:"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120",Content-Length:45,Connection:close,Host:localhost:9080,}]]|requestBody=["Check response log, contentLength=45"]
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - HTTP PUT http://service-v1/service/v1/my-endpoint
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - Accept=[application/json, application/*+json]
2024-05-15 21:20:30.184 DEBUG 75987 [da4509e7-9d23-4a49-9565-7345937c2ab1] [Pf7lpNNjRcShs6YA0Dkw5A] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.RestTemplate - Writing [UpdateRequest{id=13938, itemIds=[108067]}] as "application/json"
2024-05-15 21:20:30.216 INFO  76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [QdMxDbpHTKWZnulKulVAYA] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=REQUEST|requestMethod=PUT|requestUri=http://service-v1/service/v1/my-endpoint|discoveredRequestUri=http://-.com:11492/service/v1/my-endpoint|requestBody=[{"id":13938,"itemIds":[108067]}]|requestHeaders={x-url-encoded-single:[true],}|requestMaxReadTimeOut=300000
2024-05-15 21:20:30.216 INFO  76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [QdMxDbpHTKWZnulKulVAYA] [app] [/api/my-endpoint] - [utor-thread-100] com.ha.rest.util.RestLoggingHelper - restType=RESPONSE|requestUri=http://service-v1/service/v1/my-endpoint|responseCode=415 UNSUPPORTED_MEDIA_TYPE|responseStatus=Unsupported Media Type|responseBody=[null]
2024-05-15 21:20:30.216 DEBUG 76019 [da4509e7-9d23-4a49-9565-7345937c2ab1] [] [app] [/api/my-endpoint] - [utor-thread-100] com.RestTemplate - Response 415 UNSUPPORTED_MEDIA_TYPE
2024-05-15 21:20:30.266 ERROR 76069 [da4509e7-9d23-4a49-9565-7345937c2ab1] [] [app] [/api/my-endpoint] - [utor-thread-100] app.utils.ExceptionUtils - JSON Processing Exception

I’ve been looking for a full day and cannot seem to find where the issue might be. Any guidance would be greatly appreciated.

LEAVE A COMMENT