使用请求模块在Node.js中发送URL编码的参数

问题描述 投票:6回答:2

我正在尝试create a new paste using the PasteBin APIrequest模块如下:

var request = require("request");
request({
    url : "http://pastebin.com/api/api_post.php",
    method : "POST",
    qs : {
        "api_dev_key" : MY_DEV_KEY,
        "api_option" : "paste",
        "api_paste_code" : "random text"
    }
},function(err,res,body){
    ...
});  

我的理解是,由于该方法是POST并且提供了查询字符串参数,因此qs对象中的值将作为key=value对存储在正文中。 (参考:How are parameters sent in an HTTP POST request?

但是,我从PasteBin回来了一个Bad API request, invalid api_option。所以我qazxs wwied来自我的终端的请求如下:

curl

这很有效。

所以这导致了两个问题:

  1. 当发出curl -X POST "http://pastebin.com/api/api_post.php" -d "api_dev_key=[MY_DEV_KEY]&api_option=paste&api_paste_code=some+random+text" 请求并提供POST时,参数的确切信息是什么?
  2. 如何仅使用qs模块发送URL编码的正文?
node.js request npm pastebin
2个回答
8
投票

request键重命名为对象中的qsform密钥用于指定URL末尾的查询字符串(例如,对于GET请求)。 qs密钥用于指定表单URL编码的请求主体(例如,对于POST请求)。


0
投票

对我来说同样的问题和我的解决方案对我来说是完美的。

form
© www.soinside.com 2019 - 2024. All rights reserved.