M3105 M3105 - 4 months ago 7
JSON Question

Making a GET request JSON with parameters using Python

I was wondering how do I make a GET request to a specific url with two query parameters? These query parameters contain two id numbers
So far I have:

import json, requests
url = 'http://'
requests.post(url)


But they gave me query paramters first_id=### and last_id=###. I don't know how to include these parameters?

Answer

To make a GET request you need the get() method, for parameters use params argument:

response = requests.get(url, params={'first_id': 1, 'last_id': 2})

If the response is of a JSON content type, you can use the json() shortcut method to get it loaded into a Python object for you:

data = response.json()
print(data)
Comments