javascript 将 cURL 请求转换为 node.js 的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30491525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert cURL request to request of node.js
提问by Ilshat
Here is my valid cURL command:
这是我的有效 cURL 命令:
curl 'https://www.example.com/api/' --data '{"jsonrpc":"2.0","method":"getObjectsByFilter","id":"3"}'
here is what I tried in Node.js:
这是我在 Node.js 中尝试过的:
var url = 'https://www.example.com/api/';
var data = {
"jsonrpc":"2.0",
"id":"3"
};
req.post({ url: url, form: data}, function (err, result, body) {
But it is not valid.
但它无效。
回答by Harvey
You can use the following tool to convert and get the code:
您可以使用以下工具进行转换并获取代码:
https://curl.trillworks.com/#node
https://curl.trillworks.com/#node
They support:
他们支持:
- Node.js
- Python
- PHP
- 节点.js
- Python
- PHP
回答by Arian Faurtosh
You are going to need to install the npm module request
您将需要安装 npm 模块请求
If you have npm
installed already, just run the following command:
如果您已经npm
安装,只需运行以下命令:
npm install request
Make sure you require the module at the top of your node file, like so
确保您需要节点文件顶部的模块,就像这样
var request = require('request');
You can use the module with the following:
您可以通过以下方式使用该模块:
var request = require('request');
var url = 'https://www.example.com/api/';
var data = {
"jsonrpc":"2.0",
"id":"3"
};
request.post({url:url, formData: data}, function(err, httpResponse, body) {
if (err) {
return console.error('post failed:', err);
}
console.log('Post successful! Server responded with:', body);
});
Check the documentation out for more information:
查看文档以获取更多信息: