【发布时间】:2020-01-14 21:22:47
【问题描述】:
Within the Request Documentation 它记录了设置“useQuerystring”,内容如下:
useQuerystring - if true, use querystring to stringify and parse querystrings, otherwise use qs (default: false). Set this option to true if you need arrays to be serialized as foo=bar&foo=baz instead of the default foo[0]=bar&foo[1]=baz.
我正在使用的 API 需要向同一个参数名称发送多个值;这个设置似乎在使用 useQueryString 时可以完成,但我似乎无法弄清楚在哪里/如何传递查询字符串以便正确处理它以及在什么格式内执行它。
例如我在下面的代码中为查询字符串尝试了许多不同的地方:
let options = {
uri: "https://api.mysite.com"[0],
headers: {
'Authorization': 'Bearer TOKEN_VALUE'
},
json: true,
useQuerystring: true,
querystring: "foo=bar&foo=baz",
qs: "foo=bar&foo=baz",
proxy: "http://localhost:8080",
strictSSL: false,
};
request.get(options);
当我在“查询字符串”选项中传递查询字符串时,它会忽略它(又名,它似乎是错误的位置)。当我把它放在“qs”的“正常”位置时;我最终将以下 URL 发送到服务器:
"?0=f&1=o&2=o&3=%3D&4=b&5=a&6=r&7=%26&8=f&9=o&10=o&11=%3D&12=b&13=a&14=z"
使用设置为 true 时传递查询字符串的正确方法是什么?
根据以下文件,它看起来并没有改变查询字符串的位置,所以我认为它是 qs;但如上所述,这不起作用: https://github.com/request/request/blob/master/lib/querystring.js
感谢您的帮助!
【问题讨论】:
标签: node.js node-request