Javascript 在NodeJS中发送POST请求时如何设置Content-Length?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8262410/
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
How to set Content-Length when sending POST request in NodeJS?
提问by ekanna
var https = require('https');
var p = '/api/username/FA/AA?ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
var https = require('https');
var options = {
host: 'reportsapi.zoho.com',
port: 443,
path: p,
method: 'POST'
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
When i run the above code i am getting below error.
当我运行上面的代码时,我遇到了以下错误。
error message:
错误信息:
statusCode: 411
headers: { 'content-type': 'text/html',
'content-length': '357',
connection: 'close',
date: 'Thu, 24 Nov 2011 19:58:51 GMT',
server: 'ZGS',
'strict-transport-security': 'max-age=604800' }
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
411 - Length Required
How to fix the abobe error?
I have tried doing below
如何修复 abobe 错误?
我试过在下面做
var qs = 'ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
'
options.headers = {'Content-Length': qs.length}
But if I try this way I am getting below error:
但是,如果我尝试这种方式,则会出现以下错误:
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'socket hang up' }
Can anybody help me on this?
有人可以帮我吗?
Thanks
koti
由于
KOTI
PS:If I enter the whole url into browser address bar and hit enter I am getting JSON response as expected.
PS:如果我在浏览器地址栏中输入整个 url 并按回车键,我会按预期获得 JSON 响应。
回答by John Clements
It turns out that the solution to the given problem, when you dowant to make a POST request, is apparently to set the "headers" field of the options object to contain a 'Content-Length' field.
事实证明,当您确实想要发出 POST 请求时,给定问题的解决方案显然是将选项对象的“headers”字段设置为包含“Content-Length”字段。
See code here:
在这里查看代码:
回答by gtrenco
i think you're missing two things. Assuming pis both your endpointand your url-encoded payload.
我认为你错过了两件事。假设p既是您的端点又是您的url 编码的 payload。
You could split your p variable into the both api path, and the post_data payload you need to write before ending the request.
您可以将 p 变量拆分为 api 路径和在结束请求之前需要写入的 post_data 有效负载。
var p = 'ZOHO_ACTION=EXPORT&ZOHO_OUTPUT_FORMAT=JSON&ZOHO_ERROR_FORMAT=JSON&ZOHO_API_KEY=dummy1234&ticket=dummy9876&ZOHO_API_VERSION=1.0';
var https = require('https');
var options = {
host: 'reportsapi.zoho.com',
port: 443,
path: '/api/username/FA/AA',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(p)
}
}
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(p);
req.end();
Hope it helps!!
希望能帮助到你!!
回答by madz
var server = http.createServer();
server.on('request', function(req, res) {
req.on('data',function(data){
res.writeHead(200, {'Content-Type': 'text/plain','Content-Length':data.toString().length+''});
res.write(data.toString());
res.end();
});
});
回答by ekanna
I am able to solve this problem by changing the method from POST to GET
我可以通过将方法从 POST 更改为 GET 来解决此问题
Thanks koti
谢谢科蒂