【问题标题】:Request body is empty after POST request on nodejsnodejs上的POST请求后请求正文为空
【发布时间】:2018-06-16 16:26:27
【问题描述】:

我正在尝试来自我的 test.js 的以下发布请求,但可惜我在等待 2 小时以来一直在尝试,等待我的测试成功但它没有成功。

请用nock找到下面的代码。

beforeEach(() => {
  var payload = {
    channel: 'Cricket',
    codes: 'c--HOjyWVww:APA'
  };
  scope = nock('http://localhost:3000')
    .post('/channel/subscribe', payload)
    .reply(200, { status: 200 });
});
request.post('http://localhost:3000/channel/subscribe', function(err, body) {
  console.log(err, null);
});

it('Subscribing to channel', function(done) {
      request.post({
        url: 'http://localhost:3000/channel/subscribe',
        headers: {
          'accept': 'application/json',
          'content-Type': 'application/x-www-form-urlencoded'
        },
        mode: 'no-cors',
        body: JSON.stringify({
          channel: "Cricket",
          codes: "c--HOjyWVww:APA"
        })
      }, function(err, response) {
        console.log(request.payload);
        console.log("+response status code:", response.statusCode);
        console.log(response.failureCount);
        if (err) {
          throw err; // => Error: Nock: No match for request POST http://foobar.com/ {"foo":"biz"}
        }
        done();
      })

【问题讨论】:

    标签: node.js unit-testing request hapijs nock


    【解决方案1】:

    您是否在应用程序中设置了 bodyParser? 喜欢:

    app.use(bParser.json());
    

    在你自己的情况下应该是:

    app.use(bodyParser.urlencoded());
    app.use(bodyParser.json());
    

    【讨论】:

    • 您好,代码不在express js中,它与nodejs代码一起
    猜你喜欢
    • 2019-06-26
    • 2023-03-28
    • 2019-11-18
    • 2016-06-01
    • 1970-01-01
    • 2021-03-20
    • 2018-11-28
    • 2020-02-10
    • 1970-01-01
    相关资源
    最近更新 更多