javascript 如何在nodejs中使用请求npm发送原始放置数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21055954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do send raw put data with request npm in nodejs
提问by malukisses
I need to hit an api using "require" npm in node. The api requires raw put data (not put fields). How do I do this using request npm?
我需要在 node.js 中使用“require” npm 来访问 api。api 需要原始放置数据(不是放置字段)。如何使用请求 npm 执行此操作?
example raw put data I need to send:
我需要发送的示例原始放置数据:
var body = {
"id": 123,
"squares": [
{
square_id: 345,
color: "#ccc"
},
{
square_id: 777,
color: "#fff"
}
]
}
I'm trying this but it's not working:
我正在尝试这个,但它不起作用:
request({
method: "PUT",
uri: UPDATE_GAME,
multipart: [{
'content-type': 'application/json',
body: JSON.stringify(body)
}]
}
回答by Michael Angstadt
If you dig into the code, you'll see that for the most basic of POST/PUT operations you can use the json options parameter. It will also do the JSON.stringify() for you - your code becomes simply:
如果您深入研究代码,您会发现对于最基本的 POST/PUT 操作,您可以使用 json 选项参数。它还会为您执行 JSON.stringify() - 您的代码变得很简单:
request({
method: "PUT",
uri: UPDATE_GAME,
json: body
});
回答by Quentin
body
is a JavaScript object. You are claiming to be sending JSON.
body
是一个 JavaScript 对象。您声称要发送 JSON。
Pass it through JSON.stringify()
.
通过它JSON.stringify()
。