i'm trying to do a HTTPS REST request within nodejs using following Code:
var querystring = require('querystring');
var https = require('https');
var postData = {
'Value1' : 'abc1',
'Value2' : 'abc2',
'Value3' : '3'
};
var postBody = querystring.stringify(postData);
var options = {
host: 'URL'
port: 443,
path: 'PATH'
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postBody.length
}
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(postBody);
req.end();
req.on('error', function(e) {
console.error(e);
});
The request works but not as expected. The Body will is not send in JSON Format and Looks like:
RequestBody":"Value1=abc1&Value2=abc2&Value3=3
The Output should look like that:
RequestBody":"[\r\n {\r\n \"Value3\": \"3\",\r\n \"Value2\": \"abc2\",\r\n \"Value1\": \"abc1\"\r\n }\r\n]
I think it has something to do with stringify, maybe I have to convert it to JSON Format anyhow..
i'm trying to do a HTTPS REST request within nodejs using following Code:
var querystring = require('querystring');
var https = require('https');
var postData = {
'Value1' : 'abc1',
'Value2' : 'abc2',
'Value3' : '3'
};
var postBody = querystring.stringify(postData);
var options = {
host: 'URL'
port: 443,
path: 'PATH'
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postBody.length
}
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(postBody);
req.end();
req.on('error', function(e) {
console.error(e);
});
The request works but not as expected. The Body will is not send in JSON Format and Looks like:
RequestBody":"Value1=abc1&Value2=abc2&Value3=3
The Output should look like that:
RequestBody":"[\r\n {\r\n \"Value3\": \"3\",\r\n \"Value2\": \"abc2\",\r\n \"Value1\": \"abc1\"\r\n }\r\n]
I think it has something to do with stringify, maybe I have to convert it to JSON Format anyhow..
Share Improve this question edited Sep 25, 2017 at 10:14 David Wepunkt asked Sep 25, 2017 at 10:09 David WepunktDavid Wepunkt 311 silver badge4 bronze badges3 Answers
Reset to default 2you need to change content-type.try like this
var querystring = require('querystring');
var https = require('https');
var postData = {
'Value1' : 'abc1',
'Value2' : 'abc2',
'Value3' : '3'
};
var postBody = postData;
var options = {
host: 'URL'
port: 443,
path: 'PATH'
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
var req = https.request(options, function(res) {
console.log(res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.write(postBody);
req.end();
req.on('error', function(e) {
console.error(e);
});
I solved My Problem with the following.
jsonObject = JSON.stringify({
'Value1' : 'abc1',
'Value2' : 'abc2',
'Value3' : '3'
});
var postheaders = {
'Content-Type' : 'application/json',
'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};
In the request's headers is specified the 'Content-Type': 'application/x-www-form-urlencoded'
. You can try changing this to 'Content-Type': 'application/json'
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745136918a4613247.html
评论列表(0条)