others-how to solve spring rest controller web service curl 415 error?
1. Purpose
In this post, I will show you how to solve spring rest controller web service curl 415 error.
Here is the code:
package com.nils.microservices.hello.rest;
import com.nils.microservices.hello.domain.UserDetail;
import com.nils.microservices.hello.domain.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;
@RequiredArgsConstructor
@RestController
public class HelloRestService {
@PostMapping("queryUser1")
public @ResponseBody UserDetail sayHelloRest(@RequestBody UserInfo userInfo) {
UserDetail userDetail = new UserDetail();
userDetail.setDetail1(getBigUserDetailInfo(userInfo.getId()));
userDetail.setDetail2(getBigUserDetailInfo(userInfo.getId()));
userDetail.setDetail3(getBigUserDetailInfo(userInfo.getId()));
userDetail.setDetail4(getBigUserDetailInfo(userInfo.getId()));
return userDetail;
}
private String getBigUserDetailInfo(String id) {
StringBuffer buf = new StringBuffer();
for(int i=0;i<1000;i++) {
buf.append(id+",");
}
return buf.toString();
}
}
the command:
curl -X POST http://localhost:18500/queryUser1 -d '{"id":"12"}'
the result error:
{"timestamp":"2023-09-08T06:51:12.223+00:00","status":415,"error":"Unsupported Media Type","path":"/queryUser1"}
2. Solution
2.1 What is 415 error?
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request’s indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly
415 Unsupported Media Type is an error status code of the HTTP protocol that indicates that the server refuses to accept the client’s request because it does not support the format of its payload. It belongs to the HTTP response status codes in the 4xx category, also known as client error. Fixing a 415 error can be time-consuming for the client because it indicates that the problem is that the client sent something that the web server cannot handle.
Here are the three most common ways to fix 415 Unsupported Media Type:
- Make sure you are sending the correct Content-Type header value.
- Confirm that the server can handle the value defined in the Content-Type header.
- Check the Accept header to see what the server can handle.
2.2 The solution
The solution:
curl -X POST -H "Content-Type: application/json" http://localhost:18500/queryUser1 -d '{"id":"12"}'
Then we got this:
* About to connect() to localhost port 18500 (#0)
* Trying ::1...
* Connected to localhost (::1) port 18500 (#0)
> POST /queryUser1 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:18500
> Accept: */*
> Content-Type: application/json
> Content-Length: 11
>
* upload completely sent off: 11 out of 11 bytes
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Fri, 08 Sep 2023 07:02:22 GMT
<
{"detail1":"12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,1...
So the reason of this problme is that you must tell the spring boot server the request format you sent to it.
3. Summary
In this post, I demonstrated how to solve http code 415 error. That’s it, thanks for your reading.