我正在尝试使用Axios将带有JSON正文的POST请求发送到Spring引导应用程序。但是,Spring启动应用程序返回Required request body is missing:
。
当我向邮递员发送相同的请求时,它确实可以正常工作。
我有一个非常简单的控制器:
@RestController
@RequestMapping(value = "/deployments")
public class DeploymentController {
@PostMapping
public ResponseEntity<Deployment> createDeployment(@RequestBody Deployment deployment) {
//DoStuff
}
}
我发布的正文在两种情况下都是相同的:
{
"name": "testdeployment2",
"domainName": "domain.local",
"repository": "KB",
"branch": "master",
"servicepack": "6.5.1"
}
当我向邮递员发送请求时,请求看起来像:
{
"args": {},
"data": "{\"name\":\"testdeployment2\",\"domainName\":\"domain.local\",\"repository\":\"KB\",\"branch\":\"master\",\"servicepack\":\"6.5.1\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Cache-Control": "no-cache",
"Content-Length": "129",
"Content-Type": "application/json",
"Host": "httpbin.org",
"Postman-Token": "238d83b0-1f14-4828-abf5-45978a80c008",
"User-Agent": "PostmanRuntime/7.22.0",
"X-Amzn-Trace-Id": "Root=1-5e6221c7-9be7dcc1163e105cd3495bf8"
},
"json": {
"branch": "master",
"domainName": "domain.local",
"name": "testdeployment2",
"repository": "KB",
"servicepack": "6.5.1"
},
"origin": "IP",
"url": "http://httpbin.org/post"
}
Axios代码:
const url = 'http://httpbin.org/post';
const options = {
headers: {
'Content-Type' : 'application/json'
}
};
this.$http.post(
url,
this.form,
options
).then(result => {
this.result = result;
}).catch(error => {
alert(error);
});
并且Axios请求看起来像:
{
"args": {},
"data": "{\"name\":\"testdeployment2\",\"domainName\":\"domain.local\",\"repository\":\"KB\",\"branch\":\"master\",\"servicepack\":\"6.5.1\"}",
"files": {},
"form": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.9,nl;q=0.8",
"Content-Length": "129",
"Content-Type": "application/json",
"Dnt": "1",
"Host": "httpbin.org",
"Origin": "http://localhost:8080",
"Referer": "http://localhost:8080/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e6223c7-35d5028ab672229267f65e9c"
},
"json": {
"branch": "master",
"domainName": "domain.local",
"name": "testdeployment2",
"repository": "KB",
"servicepack": "6.5.1"
},
"origin": "IP",
"url": "http://httpbin.org/post"
}
这里有什么我没看到的东西吗?为什么Postman可以工作,而Axios不能工作?
所以我完全忘记了在API方面检查CORS。在我的控制器方法上允许所有CORS之后,它现在可以工作了。 h!