[英]Send a variable to form parameter in request HTTP POST node js
我正在嘗試通過Node js發送HTTP POST請求。
這是我的代碼:
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);
});
當我以表格形式發送“ tentativa”變量時,請求已發送但未保存任何內容。 但是,當我發送“ tentativa”變量的輸出時,它可以正常工作。 不應該一樣嗎?
這不是有效的json
var tentativa = "{ id:'" + result.rows[i][0] + "', timestamp:'" + result.rows[i][1]+"', application: '"+ applicationName +
"', type:'Log', source: '" + result.rows[i][2] + "', " + string+ " }";
最后一個值缺少key屬性,這可能就是為什么它不隨請求一起發送對象的原因。
而且,由於看起來您想要發送一個簡單的json,因此您可以將對象放入正文中。
request({
uri: "http://localhost:5000/logs",
method: "POST",
body: tentativa,
json: true,
headers: { "Content-Type": "application/json"}
}
}, function(error, response, body) {
console.log(body);
});
非常感謝幫忙。 使用javascript對象而不是該字符串解決了該問題,例如@varbrad說。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.