How to set Content-Length when sending a POST request to NodeJS?

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 get below the 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 abobe error?
I tried to do 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 get below error:

 { stack: [Getter/Setter], arguments: undefined, type: undefined, message: 'socket hang up' } 

Can someone help me with this?

thanks
koti

PS: If I enter the entire URL into the address bar of the browser and press enter, I get a JSON response as expected.

+7
source share
4 answers

It turns out that the solution to this problem, when you want to make a POST request, apparently, should set the "headers" field of the options object to the "Content-Length" field.

See the code here:

How to make an HTTP POST request in node.js?

+6
source

I think you are missing two things. Assuming p is your endpoint and your url encoded payload .

You can split your p variable into both api paths and the post_data payload you need to write before the request ends.

 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 this helps!

+3
source
 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(); }); }); 
+2
source

I can solve this problem by changing the method from POST to GET

Thanks cat

-3
source

All Articles