3

How can I get respon JSON dictionaries from server with POST:

import json
import requests

url = 'http://apiurl.com'        
parameters = {'code':1,
              'user': 'username',
              'password': 'password'
              }

headers = {'content-type': 'application/json'} 
response = requests.post(url, data = json.dumps(parameters),headers=headers)
print(response)

output: Response [200]

2 Answers 2

2
response = requests.post(url, data=json.dumps(parameters), headers=headers)
print(response)
print(response.text)
Sign up to request clarification or add additional context in comments.

3 Comments

<Response [200]> Traceback (most recent call last): File "/home/barry/Documents/Project/treeview.py", line 31, in <module> test2() File "/home/barry/Documents/Project/treeview.py", line 27, in test2 print (response.txt) AttributeError: 'Response' object has no attribute 'txt'
@BarryVassyah - it is print(response.text) and not print(response.txt). Please check the spelling.
Have you tried response.text ? It seems you have miscoded it as response.txt.
1

Since, you are going to receive a JSON object you can simply use request's built-in JSON decoder Simply do:

j = response.json()

2 Comments

i got erorr : ValueError: Expecting value: line 1 column 1 (char 0)
@BarryVassyah - I guess, you'll face an issue converting response.text to a parse-able JSON object. Let me know if you face an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.