8

我正在尝试使用带有模块 的 PasteBin API 创建一个新的粘贴,如下所示:request

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在正文中。(参考:如何在 HTTP POST 请求中发送参数?

但是,我Bad API request, invalid api_option从 PasteBin 得到了一个。所以我curl从我的终端编辑了请求,如下所示:

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"  

这行得通。

所以这导致了两个问题:

  1. POST发出并提供请求时,参数究竟是如何发送的qs
  2. 如何仅使用request模块发送 URL 编码的正文?
4

2 回答 2

12

重命名对象中的qsformqs关键是在 URL 的末尾指定查询字符串(例如对于 GET 请求)。关键是用于指定表单 URL 编码的form请求正文(例如,对于 POST 请求)。

于 2015-07-27T17:03:54.687 回答
3

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

request.post({
headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
},
url : "http://pastebin.com/api/api_post.php",
body : "api_dev_key=MY_DEV_KEY&api_option=paste&api_paste_code=andom text"},function(err,res,body){  ...});  
于 2019-04-05T07:48:44.890 回答