admin管理员组文章数量:1202599
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',
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' }
".dtd">
411 - Length Required
How to fix the abobe error?
I have tried doing below
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
PS:If I enter the whole url into browser address bar and hit enter I am getting JSON response as expected.
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
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
PS:If I enter the whole url into browser address bar and hit enter I am getting JSON response as expected.
Share Improve this question edited Nov 25, 2011 at 10:21 ekanna asked Nov 24, 2011 at 20:58 ekannaekanna 5,7128 gold badges29 silver badges31 bronze badges 1- 2 please do not use data.length, I bumped to this issue and author said do not use data.length, instead, use Buffer.byteLength(data). Ref question: stackoverflow.com/questions/18692580/… and ref issue: github.com/visionmedia/express/issues/1749 – Nam Nguyen Commented Sep 9, 2013 at 7:38
4 Answers
Reset to default 9It turns out that the solution to the given problem, when you do want to make a POST request, is apparently to set the "headers" field of the options object to contain a 'Content-Length' field.
See code here:
How to make an HTTP POST request in node.js?
i think you're missing two things. Assuming p is both your endpoint and your url-encoded 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.
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!!
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();
});
});
I am able to solve this problem by changing the method from POST to GET
Thanks koti
本文标签: javascriptHow to set ContentLength when sending POST request in NodeJSStack Overflow
版权声明:本文标题:javascript - How to set Content-Length when sending POST request in NodeJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738600750a2102051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论