首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从邮递员(但不是从自动化测试中)向NodeJS中的API发出请求的空体

从邮递员(但不是从自动化测试中)向NodeJS中的API发出请求的空体
EN

Stack Overflow用户
提问于 2017-01-03 21:04:07
回答 1查看 1.9K关注 0票数 1

在NodeJS API中通过邮递员发送请求时,我接收空的身体.(我确实收到了这个:{}),但是,从自动化测试来看,它工作得很好。实际上,当我以“表单”或" raw“的形式发送它或以"text”作为“raw”发送它时,如果我将它作为"JSON“发送到raw中,它就会冻结在”加载.“中。

当我搜索时,我读到了关于添加这两行与正文解析相关的内容,它使它在测试中有效,但对于Postman则不行:

代码语言:javascript
复制
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

整个代码如下:https://github.com/nemenosfe/TakeMe-Api,但这3个密钥文件如下(简化):

app.js

代码语言:javascript
复制
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;

路由/用户

代码语言:javascript
复制
"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

测试/用户

代码语言:javascript
复制
"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)
    });
  });

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-03 21:16:06

确保将POST请求以x-www-form-urlenconded的形式发送到邮递员中

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41452342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档