【发布时间】:2020-04-29 02:13:30
【问题描述】:
我要发送一个HTTP请求,相当于下面的curl命令:
curl --data "..." -H "Content-Type: application/json" -X POST localhost:8545
这是我在 NodeJS 中获得大部分内容的方法:
let http = require("http");
let data = "...";
let options = {
protocol: "http:",
host: "localhost",
port: 8545,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data)
}
};
let req = http.request(options, function(res) {
...
});
req.on("error", function(e) {
console.log('problem with request: ' + e.message);
});
req.write(data);
req.end();
如何添加出现在curl 命令上的-H 和-X 开关?
curl开关的描述可以在here找到。
http.request的描述可以在here找到。
但我无法得出结论如何简单地将这两个开关添加到我的 HTTP 请求中。
【问题讨论】:
-
你已经这样做了......