POST 中的空主体从 Postman 向 NodeJS 中的 API 请求(但不是来自自动化测试)

Empty body in POST requests to an API in NodeJS from Postman (but not from automated tests)

在 POST 通过 NodeJS 中的 Postman 发出的请求 API 中,我收到了空体...(我收到的正是这个:{}),但是从自动化测试来看,它工作得很好。实际上,当我将它作为 "form" 或 "raw" 与 "text" 一起发送时,发生在 Postman 中,但如果我将其作为原始 "JSON" 发送,它只会冻结在 "loading..."
当我搜索时,我读到关于添加与正文解析相关的这两行它使其适用于测试但不适用于邮递员:

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

完整代码在这里:https://github.com/nemenosfe/TakeMe-Api 但是3个关键文件如下(简化):

app.js:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const cors = require('cors');
const user = require('./routes/users');

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.use('/users', user);

app.use(cors());

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
});

app.listen(8888, function() {
  console.log("Node server running on http://localhost:8888");
});

module.exports = app;

routes/users:

"use strict"
const express = require('express'),
      router = express.Router(),
      /* ... many require and other code probably not relevant for the problem ... */

router
  .post('/', function(req, res, next) {
    console.log(`conditions: ${(!req.body)} ${(!req.body.uid)} ${(!req.body.provider)} ${JSON.stringify(req.body)}`);
    // This console.log appears as follows in the console once I make the request by Postman: false true true {}
    // But it receives what it shoulds with automated tests

    if (!req.body || !req.body.uid || !req.body.provider) { utilsErrors.handleNoParams(res); }
        else {
      /* ... There is a lot of code here but it's not relevant for the problem because it doesn't even reaches this point. */
    }
  })

module.exports = router

tests/users:

"use strict"
let request = require('supertest-as-promised');
const api = require('../app');
/* ... Another require not relevant for the problem ... */
request = request(api);

describe('Users route', function() {
  describe.only('POST /users', function() {
    it("should create a new user when it doesn't exist", function(done) {
      const params = {
        'appkey': helperCommon.appkey,
        'uid': 1,
        'provider': 'providerTest',
        'name': 'fakeName'
      };
      request
        .post('/users')
        .set('Accept', 'application/json')
        .send(params)
        .expect(201)
        .expect('Content-Type', /application\/json/)
        .then((res) => {
          expect(res.body).to.have.property('user');
          const userResponse = res.body.user;
          expect(userResponse).to.have.property('uid', params.uid);
          expect(userResponse).to.have.property('provider', params.provider);
          expect(userResponse).to.have.property('name', params.name);
          /* ... other expectectations that are not important for the problem ... */
          done();
        }, done)
    });
  });

谢谢!

确保您在邮递员中以 x-www-form-urlenconded

的形式发送 POST 请求