无法在邮递员中获取请求正文

时间:2019-11-13 09:25:53

标签: node.js express post postman

我有一个控制器功能和一个端点:

控制器

uploadSalesOrderFromExcel : async function (req,res) {
        console.log(req.body);
        if (!req.body.branchId) {
            return res.status(400).send({ Error: "Branch Id is required." });
          }
          if (!req.file) {
            return res.status(400).send({ Error: "Excel file is required." });
          }
          const excelData = await readExcel(file, { sheet: 'Sales Order' })
          res.send(excelData);
        }

路线 http://localhost:8080/sp/salesOrders/uploadSalesOrder

现在从邮递员访问API后,我得到以下响应:

{
    "Error": "Branch Id is required."
}

cURL

curl -X POST \
  http://localhost:8080/sp/salesOrders/uploadSalesOrder \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic Mzo5MTQ4MDk0ZjIxMDJhM2Q3M2U0OWY5NDljMGQ2YTIzYw==' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 164' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: 6bd29ccf-ebad-4299-8687-9ea46cf35686,4ea8d417-df27-49a1-8d84-3aabddbccd2c' \
  -H 'User-Agent: PostmanRuntime/7.19.0' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F branchId=5

2 个答案:

答案 0 :(得分:0)

我认为您应该改为使用-d标志,并将branchId作为对象传递。尝试

  curl -X POST \
  http://localhost:8080/sp/salesOrders/uploadSalesOrder \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic Mzo5MTQ4MDk0ZjIxMDJhM2Q3M2U0OWY5NDljMGQ2YTIzYw==' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 164' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: 6bd29ccf-ebad-4299-8687-9ea46cf35686,4ea8d417-df27-49a1-8d84-3aabddbccd2c' \
  -H 'User-Agent: PostmanRuntime/7.19.0' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -d '{"branchId": 5}'

答案 1 :(得分:0)

我没有使用multer。这就是为什么我在请求正文中获取空白对象的原因。