1

I'm sure that this is a basic syntax question, but I am trying to hit an API that wants "a request" structured like:

{"customer":{"firstName":"Regular","lastName":"Joe"}...} 

I also want to include some options, including query parameters, i.e.:

options = {
    method: "POST"
    url: "https://api.rezdy.com/latest/bookings"
    qs: {
        apiKey: apiKey,
    }
    json: true
}

How do I include the former data in the options hash so that I can call it like so?

request(options, (err, response, body)->
    ...
)

I tried doing it with formData like so:

options = {
    method: "POST"
    url: "https://api.rezdy.com/latest/bookings"
    qs: {
        apiKey: apiKey,
    }
    formData: data
    json: true
}

but I am still getting errors back from the API suggesting that it has not received data. What is the correct way to include the data in the options hash?

fox
  • 15,428
  • 20
  • 55
  • 85

1 Answers1

1

According to official page https://www.npmjs.com/package/request

you can send formdata like this -

request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })

You can also use syntax like jquery - For example

request({
                        method: 'POST',
                        uri: 'xxxxxx',
                        body: data,
                        json: true
                    }, function (error, response, body) {
                        console.log(body);                           
                        };

For more info you can read - How to make an HTTP POST request in node.js?

Community
  • 1
  • 1
NeiL
  • 791
  • 8
  • 35