【问题标题】:Why does Spring boot not accept my Axios POST requests?为什么 Spring boot 不接受我的 Axios POST 请求?
【发布时间】:2020-06-19 01:49:23
【问题描述】:

我正在尝试使用 Axios 将带有 JSON 正文的 POST 请求发送到 Spring Boot 应用程序。但是,Spring 引导应用程序返回 Required request body is missing:

当我使用 Postman 发送相同的请求时,它确实按预期工作。

我有一个非常简单的控制器:

@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"
}

当我使用 Postman 发送请求时,请求如下所示:

{
  "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 不行?

【问题讨论】:

    标签: java spring-boot post axios postman


    【解决方案1】:

    所以我完全忘记在 API 端检查 CORS。在我的控制器方法上允许所有 CORS 后,它现在可以工作了。呵呵!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2020-04-29
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 2017-08-10
      • 2018-10-21
      相关资源
      最近更新 更多