gpt4 book ai didi

java - 将 POST 从 api 转发到另一个时出现错误代码 415

转载 作者:行者123 更新时间:2023-11-30 10:05:56 25 4
gpt4 key购买 nike

我正在将 application/json 类型从 postman 客户端发送到 java API,该 java API 将所有请求转发到该案例的特定 API。在这个具体案例中,我有一个登录 API,我想集中代码听到这个 JSON:

来自 postman 的 JSON

{
"name": "random name",
"password": "random passwd"
}

做转发的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";
}

}

新 URL 是其他 API 的 URL(在本例中,是 localhost:8080 并且来自 application.properties 文件)。

enter image description here

我已经通过 postman 测试了登录 API 并且它可以工作,但是当我尝试将它连接到那个转发 API 时,我收到以下错误:

org.springframework.web.client.HttpClientErrorException: 415 null.

我想知道我做错了什么或其他方法。

postman 电话 enter image description here

第二个端点代码 enter image description here

传递给第二个端点的body的值 enter image description here

用户类

公共(public)类用户{

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;
}

最佳答案

问题是您收到了 415 错误代码。这意味着您的 /login 端点不期望您发送给他的有效负载类型,请参阅 here

415 (Unsupported Media Type)

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. 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.

For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format.

我认为这是因为当您调用 postForObject 时,您并没有告诉您有效负载的媒体类型。因此,不是单独发送 json 字符串,而是需要将其包装到一个 HttpEntity 中,其中包含正文和一个 header , header 指定您要转发的有效负载的媒体类型。试试这个:

...
} 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);
}
...

关于java - 将 POST 从 api 转发到另一个时出现错误代码 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54982501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com