Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Json (contains boolean/numbers) data in python GET(Method) requests

Q. Is it possible to send json data with GET request?

I want to pass some filter parameters to read results from DB

params = {'is_coming_soon': False, 'is_paid': True, 'limit': 100, 'skip': 0}
headers = {'content-type': 'application/json'}
response = requests.get(url=url, headers=headers, params=params)

In server side I am receiving all values as string,

how can I make request to get proper boolean values not string one.

like image 611
OmPrakash Avatar asked Dec 24 '22 14:12

OmPrakash


1 Answers

To send a json payload use the data or json argument instead of params. Convert the dict to json first using json.dumps, and double-check the conversion if you like:

import json

payload = json.dumps(params)
print(payload)  # '{"is_coming_soon": false, "is_paid": true, "limit": 100, "skip": 0}'

response = requests.get(url=url, headers=headers, data=payload)

Also, you can debug requests calls like this:

import requests
import logging

from http.client import HTTPConnection
# from httplib import HTTPConnection  # Python 2

HTTPConnection.debuglevel = 1

logging.basicConfig() 
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

response = requests.get(url=url, headers=headers, data=payload)
like image 186
brennan Avatar answered Dec 28 '22 07:12

brennan