如何使用包含选项的request.js进行POST请求

我确定这是一个基本的语法问题,但是我试图创build一个需要“请求”结构的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这样做:

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

但是我仍然从API获取错误,提示它没有收到数据。 在选项散列中包含数据的正确方法是什么?

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

你可以像这样发送formdata –

 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请求?