[英]How can I format this as a RESTful API call?
我正在學習使用API。 我正在嘗試進行如下的api調用:
curl -X POST https://api.locu.com/v2/venue/search -d '{
"api_key" : "f165c0e560d0700288c2f70cf6b26e0c2de0348f",
"fields" : [ "name", "location", "contact" ],
"venue_queries" : [
{
"name" : "bistro central parc"
}
]
}'
這適用於curl,但我想在沒有cURL的情況下進行通話。 我現在將使用來自節點的請求來執行此操作,但是我只想使用郵遞員進行呼叫。 我努力了:
https://api.locu.com/v2/venue/search?api_key={myAPIKey}&fields=["name","location","contact"]&venue_queries=[{name="bistro%20central%20parc"}]
但是我不允許使用405方法。 我顯然是錯誤地看待這個問題。 這不是格式化http請求的正確方法嗎?
在javascript中看起來像這樣:
var request = require('request');
request('https://api.locu.com/v2/venue/search?api_key={myAPIKey}&fields=["name","location","contact"]&venue_queries=[{name="bistro%20central%20parc"}]', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
還是會有更好的方法來構造選項?
當然可以,請看一下您可以傳入的qs對象上的文檔:
qs-包含要附加到uri的querystring值的對象
var request = require('request');
var queryObject = { field1:'test1', field2:'test2' };
request.get({url: url, qs: queryObject})
.then(function(response){
// do stuff
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.