繁体   English   中英

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

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

我确定这是一个基本的语法问题,但是我试图打一个想要“请求” 的API ,其结构如下:

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

我还想包括一些选项,包括查询参数,即:

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

如何在options哈希中包含以前的数据,以便可以这样称呼它?

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

我尝试使用formData这样做, formData所示:

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

但我仍然从API那里收到错误消息,提示它尚未接收到数据。 在选项哈希中包含数据的正确方法是什么?

根据官方页面https://www.npmjs.com/package/request

您可以像这样发送表单数据-

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

您还可以使用类似jquery的语法-例如

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

有关更多信息,您可以阅读- 如何在node.js中发出HTTP POST请求?

暂无
暂无

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

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