我正在尝试使用 node.js 向 Cloud9 中的服务器发送 POST。使用 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]
我已将 node 更新为v8.15
,尝试过npm i natives
,因为这些解决方案对其他人有用,但我仍然遇到相同的错误。我也尝试过使用 request 和 Unirest 而不是 http,但是在两者发生任何事情之前连接超时。