[英]Error code 415 when forwarding a POST from an api to another
I'm sending an application/json type from the postman client to a java API that forwards all the requests to the specific API for that case.我正在将应用程序/json 类型从邮递员客户端发送到 Java API,该 API 将所有请求转发到该案例的特定 API。 On this concrete case, I have a login API and I want to center code heard this JSON:
在这个具体案例中,我有一个登录 API,我想将代码集中在这个 JSON 中:
JSON from the postman来自邮递员的 JSON
{
"name": "random name",
"password": "random passwd"
}
The API that does the forward执行转发的 API
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String redirectHttpRequest(HttpServletRequest request, @Value("${endpoint}") String newURL,
HttpServletResponse response) throws IOException {
try {
RestTemplate restTemplate = new RestTemplate();
String result = null;
String body = IOUtils.toString(request.getReader());
if (request.getMethod().equals("GET")) {
// result = restTemplate.getForObject(redirectUrl.toString(), String.class);
} else if (request.getMethod().equals("POST")) {
result = restTemplate.postForObject(newURL, body, String.class);
}
return result;
} catch (HttpClientErrorException e) {
System.out.println(e);
return "OLA";
}
}
That new URL is the URL were the other API is (which, in this case, is localhost:8080
and is from the application.properties
file).该新 URL 是其他 API 所在的 URL(在本例中为
localhost:8080
并且来自application.properties
文件)。
I've tested the login API through postman and it works, but when I try to connect it to that forward API I got the following error:我已经通过邮递员测试了登录 API 并且它可以工作,但是当我尝试将其连接到该转发 API 时,出现以下错误:
org.springframework.web.client.HttpClientErrorException: 415 null.
org.springframework.web.client.HttpClientErrorException: 415 null。
I would like to know what I am doing wrong or an alternative way to do it.我想知道我做错了什么或替代方法。
The value of the body passed to the second endpoint传递给第二个端点的正文的值
The User class用户类
public class User {公共类用户{
private String name;
private String password;
private List<String> groups;
public User(String name, String password) {
this.name = name;
this.password = password;
this.groups = new ArrayList<String>();
}
public User() {
}
public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
public List<String> getGroups() {
return this.groups;
}
public String toString() {
return "User: " + this.name + "\nGroups: " + this.groups;
}
The problem is that you are getting the 415
error code.问题是您收到了
415
错误代码。 That means that your /login
endpoint is not expecting the payload type you are sending him, see here这意味着您的
/login
端点不期望您发送给他的有效负载类型,请参见此处
415 (Unsupported Media Type)
415(不支持的媒体类型)
The 415 error response indicates that the API is not able to process the client's supplied media type, as indicated by the Content-Type request header.
415 错误响应表明 API 无法处理客户端提供的媒体类型,如 Content-Type 请求标头所示。 For example, a client request including data formatted as application/xml will receive a 415 response if the API is only willing to process data formatted as application/json.
例如,如果 API 仅愿意处理格式为 application/json 的数据,则包含格式为 application/xml 的数据的客户端请求将收到 415 响应。
For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.
例如,客户端上传图片为image/svg+xml,但是服务器要求图片使用不同的格式。
I think is this because when you call postForObject
, you are not telling the media type of your payload.我认为这是因为当你调用
postForObject
,你没有告诉你的有效负载的媒体类型。 So instead of sending the json String alone, you need to wrap it into an HttpEntity
that holds the body and also a header that specifies the media type of the payload you are forwarding.因此,不是单独发送 json 字符串,而是需要将其包装到一个
HttpEntity
,该HttpEntity
包含主体和指定要转发的有效负载的媒体类型的标头。 Try this out:试试这个:
...
} else if (request.getMethod().equals("POST")) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
HttpEntity<String> entity = new HttpEntity<>(body, headers);
result = restTemplate.postForObject(newURL, entity, String.class);
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.