I'm trying to send an HTTP POST request through Node js.
Here's my code:
var tentativa = "{ id:'" + result.rows[i][0] + "', timestamp:'" + result.rows[i][1]+"', application: '"+ applicationName +
"', type:'Log', source: '" + result.rows[i][2] + "', " + string+ " }";
request({
uri: "http://localhost:5000/logs",
method: "POST",
form: tentativa,
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }
}, function(error, response, body) {
console.log(body);
});
When I send the "tentativa" variable in the form, the request its sent but not saves anything. But when I send the output of the "tentativa" variable it work fine. Shouldn't it be the same?
this is not a valid json
var tentativa = "{ id:'" + result.rows[i][0] + "', timestamp:'" + result.rows[i][1]+"', application: '"+ applicationName +
"', type:'Log', source: '" + result.rows[i][2] + "', " + string+ " }";
the last value miss the key property, this is probably why it's not sending the object along with the request.
And since it looks you want to send a plain json, you can just put the object inside the body.
request({
uri: "http://localhost:5000/logs",
method: "POST",
body: tentativa,
json: true,
headers: { "Content-Type": "application/json"}
}
}, function(error, response, body) {
console.log(body);
});
Thanks so much for the help. Solved the problem using a javascript object instead of that string, like @varbrad said.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.