【问题标题】:HTTP POST with node.js and request library isn't outputting anything带有 node.js 和请求库的 HTTP POST 没有输出任何内容
【发布时间】:2019-03-16 21:50:35
【问题描述】:

我只是在测试我对网站的 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.js post http-post requestjs


    【解决方案1】:

    我建议你,像这样更新你的代码,然后再试一次:

    var request = require('request');
    
    var options = {
      uri: 'My_API_URL',
      method: 'POST',
      json: {
        "text":"this is my text"
      }
    };
    
    request(options, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
      }
    });
    

    如果结果相同,请告诉我。

    【讨论】:

    【解决方案2】:

    检查此link。你应该像这样发布请求:

    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);
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2012-05-11
      • 2011-02-07
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2021-02-06
      相关资源
      最近更新 更多