首页 > 解决方案 > 带有 node.js 和请求库的 HTTP POST 没有输出任何内容

问题描述

我只是在测试我对网站的 POST 请求是否有效,但它没有输出任何内容。当我使用 RunKit 时,它会显示输出,但不会在我的 powershell 中显示。我做错了什么还是没有输出?我怎样才能让它显示输出?这是我的代码:

var request = require('request');

request.post(
    'My_API_URL',
    { json: { "text":"this is my text" } },
    function (error, response, body) {
      console.log(body);
    }
);

标签: node.jsposthttp-postrequestjs

解决方案


检查此链接。您应该像这样发布请求:

var request = require('request');

var body = JSON.stringify({ 
    client_id: '0123456789abcdef', 
    client_secret: 'secret', 
    code: 'abcdef'
});

request.post({
    url: 'https://postman-echo.com/post',
    body: body,
    headers: {
      'Content-Type': 'application/json'
    }},
    function (error, response, body) {
      console.log(body);
    }
);

推荐阅读