最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS create HTTPS Rest body request JSON - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 2

you 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'.

发布评论

评论列表(0)

  1. 暂无评论