node.js - Correspondence between curl command and node's request -
how can execute curl shell command curl --data "{\"obj\" : \"1234556\"}" --digest "https://username:password@www.someurl.com/rest-api/v0/objectpost"
correctly returns expected values using node's request package? tried post options got no success:
var request = require('request'); var body = {"obj" : "1234556"}; var post_options = { url: url, method: 'post', auth: { 'user': 'username', 'pass': 'password', 'sendimmediately': false }, headers: { 'content-type': 'text/json', 'content-length': json.stringify(body).length, 'accept': "text/json", 'cache-control': "no-cache", 'pragma': "no-cache" }, timeout: 4500000, body: json.stringify(body) } request(post_options, callback);
this way body not parsed (got missing required parameter: "obj"
), , can't understand if it's matter of encoding or passing in wrong place (i.e. should not body). suggestion?
by default, curl send content-type: application/x-www-form-urlencoded
unless use -f
(which changes content-type: multipart/form-data
) fields or explicitly override header (e.g. -h 'content-type: application/json'
). however, data being sent curl example seems json. server confused , won't correctly find data it's expecting.
so solution 1 of 2 options:
try
application/json
content-type
in code instead oftext/json
.actually use urlencoded formatted data instead of json using
form
property.request
takeform
object , conversions , setting of headers, etc. you. example:var post_options = { url: url, method: 'post', auth: { user: 'username', pass: 'password', sendimmediately: false }, timeout: 4500000, form: body };
Comments
Post a Comment