简体   繁体   English

如何使用包含选项的request.js发出POST请求

[英]how to make a POST request using request.js that includes options

I'm sure that this is a basic syntax question, but I am trying to hit an API that wants "a request" structured like: 我确定这是一个基本的语法问题,但是我试图打一个想要“请求” 的API ,其结构如下:

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

I also want to include some options, including query parameters, ie: 我还想包括一些选项,包括查询参数,即:

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? 如何在options哈希中包含以前的数据,以便可以这样称呼它?

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

I tried doing it with formData like so: 我尝试使用formData这样做, formData所示:

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. 但我仍然从API那里收到错误消息,提示它尚未接收到数据。 What is the correct way to include the data in the options hash? 在选项哈希中包含数据的正确方法是什么?

According to official page https://www.npmjs.com/package/request 根据官方页面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 您还可以使用类似jquery的语法-例如

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? 有关更多信息,您可以阅读- 如何在node.js中发出HTTP POST请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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