我正在尝试使用node.js将POST发送到Cloud9中的服务器。使用Postmaster时,它可以正常工作。我已经将其翻译为node.js并添加了http模块,因此我的相关代码如下:
var http = require("http");
var options = {
"method": "POST",
"hostname": [
"192.168.153.188"
],
"port": "8080",
"path": [
"index.php"
],
"headers": {
"Content-Type": "text/plain",
"cache-control": "no-cache",
"Postman-Token": "19ed6137-291e-45e7-b4c3-04e62c0c89dc"
}
};
var req = http.request(options, function(res) {
var chunks = [];
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function() {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
发送帖子时出现以下错误:
/var/lang/bin/node[1]: ../src/tcp_wrap.cc:247:static void node::TCPWrap::Connect(const v8::FunctionCallbackInfo<v8::Value>&): Assertion `args[1]->IsString()' failed.
1: node::Abort() [/var/lang/bin/node]
2: node::Assert(char const* const (*) [4]) [/var/lang/bin/node]
3: node::TCPWrap::Connect(v8::FunctionCallbackInfo<v8::Value> const&) [/var/lang/bin/node]
4: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/var/lang/bin/node]
5: 0x55ccab57a28b [/var/lang/bin/node]
6: 0x55ccab57ac27 [/var/lang/bin/node]
我已将节点更新为v8.15
,并尝试了npm i natives
,因为这些是适用于其他人的解决方案,但我仍然遇到相同的错误。
我也尝试过使用request和Unirest代替http,但是连接超时,而这两者都没有发生。