在查询string中发布嵌套的对象 – Node.js

我的代码试图从我的本地Node.js服务器发布数据到Coldfusion API。 我已经设法与API通信,并通过请求头进行身份validation。 然而,我实际上很难通过我的JSON对象,因为我无法得到正确的结构。

API不接受请求模块的JSON选项,所以这是我最简单的select。

该API期待以下内容:

{ 'source': { 'customer': { 'customerlogin': 'myusername', 'customerpassword': 'mypassword', } } } 

我的代码工作,如果我硬编码下面的身体参数(从别人的成功后)到我的文章。

 var Jrequest = require('request'); var options = { uri: 'http://myAPI/customerSSO.json', headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': something', 'Timestamp': timestamp}, method: 'POST', body: 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' // Working }; Jrequest(options, function (error, response, body){ res.send(body); }); 

如果我通过其他方式发送JSON,例如json.stringify(),则以“源是必需的但未定义的”为由拒绝。

所以我想我的问题是,在node.js中,我怎么把JSON变成看起来像这样的东西

 'source=%7B%0D%0A++%22customer%22%3A+%7B%0D%0A++++%22customerlogin%22%3A+%22myusername%22%2C%0D%0A++++%22customerpassword%22%3A+%22mypassword%22%2C%0D%0A%09%22success%22%3A+%22%22%0D%0A++%7D%0D%0A%7D' 

还是我忽略了另一种select?

如果我使用了错误的术语,谢谢你的帮助和道歉。

我认为这应该工作:

 var querystring = require('querystring'); ... request({ ... headers : { 'Content-Type': 'application/x-www-form-urlencoded', ... }, body : 'source=' + querystring.escape(JSON.stringify({ 'customer': { 'customerlogin': 'myusername', 'customerpassword': 'mypassword', } })), ... }, ...) 

你的例子还包含换行符和回车等,但我假设这些是可选的。