Sam Parks
Sam Parks

Reputation: 43

How to send GET Request with path parameters in Python

I'm having trouble with a GET request from the EatStreet Public API using Python. I've had success with other endpoints on the site, but I'm unsure of how to deal with path parameters in this instance.

This curl request returns 200:

curl -X GET \ -H 'X-Access-Token: API_EXPLORER_AUTH_KEY' \ 'https://eatstreet.com/publicapi/v1/restaurant/90fd4587554469b1f15b4f2e73e761809f4b4bcca52eedca/menu?includeCustomizations=false'

This is what I have currently, but I keep getting error code 404. I've tried multiple other ways messing around with the parameters and headers but nothing seems to work.

api_url = 'https://eatstreet.com/publicapi/v1/restaurant/
90fd4587554469b1f15b4f2e73e761809f4b4bcca52eedca/menu'
headers = {'X-Access-Token': apiKey}

def get_restaurant_details():

        response = requests.request("GET", api_url, headers=headers)
        print(response.status_code)

        if response.status_code == 200:
                return json.loads(response.content.decode('utf-8'))
        else:
                return None

Here is a link to the EatStreet Public API: https://developers.eatstreet.com/

Upvotes: 2

Views: 16256

Answers (1)

Kmschr
Kmschr

Reputation: 86

Passing Parameters In URLs

You often want to send some sort of data in the URL’s query string. If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. httpbin.org/get?key=val. Requests allows you to provide these arguments as a dictionary of strings, using the params keyword argument. As an example, if you wanted to pass key1=value1 and key2=value2 to httpbin.org/get, you would use the following code:

    payload = {'key1': 'value1', 'key2': 'value2'}
    r = requests.get('https://httpbin.org/get', params=payload)

You can see that the URL has been correctly encoded by printing the URL:

    print(r.url)
    https://httpbin.org/get?key2=value2&key1=value1

Note that any dictionary key whose value is None will not be added to the URL’s query string.

You can also pass a list of items as a value:

    payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
    r = requests.get('https://httpbin.org/get', params=payload)
    print(r.url)
    https://httpbin.org/get?key1=value1&key2=value2&key2=value3

So in your case it might look something like

parameters = {'includeCustomizations':'false'}
response = requests.request("GET", api_url, params=parameters, headers=headers)

Upvotes: 3

Related Questions