【发布时间】:2016-02-11 18:00:43
【问题描述】:
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)
}
}
);
我想在 req 变量中发送原始请求正文。它正在使用邮递员,但在节点 js 中,我无法将原始 json 作为发布请求的请求正文发送。
【问题讨论】:
-
错误是什么?你的
"content-type": "application/x-www-form-urlencoded"可能不正确,因为你正在发送 JSON?应该是application/json。 -
POST /HTTP/JSON/Prices/GetPriceSheet.aspx HTTP/1.1 主机:host.com 内容类型:application/x-www-form-urlencoded 缓存控制:no-cache { "request ": { "header": { "username": "name", "password": "pw" }, "body": { "shape":"round" } } } 这是 postman 上的请求预览。所以我需要在标头中使用 x-www-form-urlencoded,但也需要发送原始 json 数据。我得到的错误是来自休息服务的错误格式。