简体   繁体   中英

How can I format this as a RESTful API call?

Im learning to work with APIs. Im trying to make an api call like :

curl -X POST https://api.locu.com/v2/venue/search -d '{
  "api_key" : "f165c0e560d0700288c2f70cf6b26e0c2de0348f",
  "fields" : [ "name", "location", "contact" ],
  "venue_queries" : [
    {
      "name" : "bistro central parc"
    }
  ]
}'

this works with curl but I am wanting to make the call without cURL. I will be doing this using request from node, right now however I just want to use postman to make the call. I have tried:

https://api.locu.com/v2/venue/search?api_key={myAPIKey}&fields=["name","location","contact"]&venue_queries=[{name="bistro%20central%20parc"}]

however I get a 405 method not allowed. Im obviously looking at this the wrong way. Is this not the proper way to format the http request?

In javascript would this look like:

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.
     }
})

or would there be a better way to structure the options?

You javascript's request is in GET method not post. You can try it as following:

邮差

If postman is the primary method you want, then this should be what you want. If you want to send the request programmatically in javascript (or nodejs ), then you should take care of the request method you've taken.

There certainly is, take a look at the docs on the qs object you can pass in:

qs - object containing querystring values to be appended to the uri

 var request = require('request');

 var queryObject = { field1:'test1', field2:'test2' };

 request.get({url: url, qs: queryObject})
   .then(function(response){
     // do stuff   
   })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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