Hrutvik Patel
Hrutvik Patel

Reputation: 13

How do I make a cURL request to zomato api?

I just began exploring APIs. This is my code so far. For locu API this works but for Zomato they use curl header request which I don't know how to use. Could someone guide or show me how?

import json
import urllib2

Key = 'Zomato_key'

url = 'https://developers.zomato.com/api/v2.1/categories'

json_obj = urllib2.urlopen(url)

data = json.load(json_obj)

print data

Upvotes: 0

Views: 1801

Answers (2)

neuronics_3 lab
neuronics_3 lab

Reputation: 1

this didn't work for me can u suggest some other method for me. -->the code when tried to compile is taking long time and returning an traceback error in request method that is in built

but curl command is working curl -X GET --header "Accept: application/json" --header "user-key: c5062d18e16b9bb9d857391bb32bb52f" "https://developers.zomato.com/api/v2.1/categories"

Upvotes: 0

JahMyst
JahMyst

Reputation: 1686

By looking at the Zomato API docs, it seems that the parameter user-key has to be set in the header.

The following works:

import json
import urllib2

Key = '<YOUR_ZOMATO_API_KEY>'
url = "https://developers.zomato.com/api/v2.1/categories"

request = urllib2.Request(url, headers={"user-key" : Key})
json_obj = urllib2.urlopen(request)
data = json.load(json_obj)

print data

If you want a more elegant way to query APIs, have a look at requests module (you can install using pip install requests).

I suggest you the following:

import json
import requests

Key = <YOUR_ZOMATO_API_KEY>'
url = "https://developers.zomato.com/api/v2.1/categories"

if __name__ == '__main__':
    r = requests.get(url, headers={'user-key': Key})
    if r.ok:
        data = r.json()
        print data

NB: I suggest you remove your Key from StackOverflow if you care about keeping it to yourself.

Upvotes: 3

Related Questions