简体   繁体   中英

How do send raw put data with request npm in nodejs

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?

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. It will also do the JSON.stringify() for you - your code becomes simply:

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

body is a JavaScript object. You are claiming to be sending JSON.

Pass it through JSON.stringify() .

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.

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