Reputation: 2210
I need to pass a dictionary for my get requests, but when executed like this I get a compilation error :
patVect = {"poo": 0, "pin": 0, "pok": 0, "pat": 0}
# querystring = {"patients": "{\"poo\":0, \"pin\":0, \"pok\":0, \"pat\":0}"}
querystring = {"patients": patVect}
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
}
response = requests.api.request('get', HURL, headers=headers, params=querystring, verify=False)
when I am working with the commented query that works fine. any idea why that doesn't work, or a function that will help.
Upvotes: 2
Views: 11002
Reputation: 111
You can test that API using Postman, you can pass the dict
parameter as raw JSON
in the body section.
Then when you have tested the API call you can go ahead and generate the code for it in your preferred language.
This is as Python Requests
:
import requests
url = "http://localhost:8000/foo/bar/"
payload = "{\n\t\"dict\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}"
headers = {
'Content-Type': "application/json",
'cache-control': "no-cache",
'Postman-Token': <TOKEN>
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response.text)
This is as Python http.client(Python3)
:
import http.client
conn = http.client.HTTPConnection("localhost")
payload = "{\n\t\"dict\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}"
headers = {
'Content-Type': "application/json",
'cache-control': "no-cache",
'Postman-Token': <TOKEN>
}
conn.request("GET", "foo,bar,", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Upvotes: 1