Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
            }]
        }
like image 606
malukisses Avatar asked Jan 10 '14 22:01

malukisses


1 Answers

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
 });
like image 193
Michael Angstadt Avatar answered Oct 03 '22 06:10

Michael Angstadt