简体   繁体   English

如何在nodejs中发送带有请求npm的原始数据

[英]How do send raw put data with request npm in nodejs

I need to hit an api using "require" npm in node. 我需要在节点中使用“require”npm命中api。 The api requires raw put data (not put fields). api需要原始数据(不是put字段)。 How do I do this using request npm? 如何使用请求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)
            }]
        }

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. 如果深入研究代码,您会发现对于最基本的POST / PUT操作,您可以使用json选项参数。 It will also do the JSON.stringify() for you - your code becomes simply: 它还将为您执行JSON.stringify() - 您的代码变得简单:

request({
  method: "PUT",
  uri: UPDATE_GAME,
  json: body
 });

body is a JavaScript object. body是一个JavaScript对象。 You are claiming to be sending JSON. 您声称要发送JSON。

Pass it through JSON.stringify() . 通过JSON.stringify()传递它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM