Search code examples
javascriptnode.jshttphttp-headersnpm-request

How to form url and send http request with data in Node.js


i want to form url with below "Raw Request" then send request to it in nodejs,how can i do this.


Raw Request:
-----------

POST /sky?api_key="" HTTP/1.1
Host: https://sky.in
Content-Type: application/json
Connection: close
Content-Length: 82
{"registration_ids":[""], "data":{"message":"Hello World!"}}


Thank you.


Solution

  • You can use the request module:

    var request = require('request')
    request.post('https://sky.in/sky', {
      qs: {
        api_key: ''
      },
      json: {
        registration_ids: [],
        data: {message: 'Hello World!'}
      },
    }, function (err, res, body) {
      // body contains the parsed JSON response
    })