var req ={
"request": {
"header": {
"username": "name",
"password": "password"
},
"body": {
"shape":"round"
}
}
};
request.post(
{url:'posturl',
body: JSON.stringify(req),
headers: { "content-type": "application/x-www-form-urlencoded"}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
I want to send raw request body in req variable . It is working on postman but in node js i am not able to send the raw json as request body for post request .
You are trying to send JSON (your req
variable) but you are parsing it as a String (JSON.stringify(req)
). Since your route is expecting JSON, it will probably fail and return an error. Try the request below:
request.post({
url: 'posturl',
body: req,
json: true
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
Instead of setting your headers, you can just add the option json: true
if you are sending JSON.
Change the Content-Type
to application/json
, since your body is in JSON format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With