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)
            }]
        }

Answers:

Answer

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
 });
Answer

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

Pass it through JSON.stringify().

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.