4

This might be a simple question but I couldn't find the problem why I'm not able to call post request to API URL. I have cross-checked with similar questions but mine still has a problem.

This is the script:

import requests import json #API details url = "http://192.168.1.100:9792/api/scan" body = {"service":"scan", "user_id":"1", "action":"read_all", "code":"0"} headers = {'Content-Type': 'application/json'} #Making http post request response = requests.post(url, headers=headers, data=body, verify=False) print(response) #Decode response.json() method to a python dictionary for data process utilization dictData = response.json() print(dictData) with open('scan.json', 'w') as fp: json.dump(dictData, fp, indent=4, sort_keys=True) 

Getting error

 raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded 

print(response) got return

<Response [200]> 

If I run curl like below ok.. and it will return the data from API post request:

curl --header "Content-Type: application/json" --request POST --data '{"service":"scan","user_id":"1","action":"read_all","code":"0"}' http://192.168.1.100:9792/api/scan 

using curl ok... but when I use requests/json Python got problem... I think I might miss something here where I'm not able to detect. Please help and point me the right way.

2
  • What does response.text contains? Commented Nov 11, 2019 at 6:34
  • it give nothing , just blank line... Commented Nov 11, 2019 at 6:36

2 Answers 2

8

I had similar errors and dumping my data solved the issue. Try passing your body as a dump instead:

 import requests import json #API details url = "http://192.168.1.100:9792/api/scan" body = json.dumps({"service":"scan", "user_id":"1", "action":"read_all", "code":"0"}) headers = {'Content-Type': 'application/json'} #Making http post request response = requests.post(url, headers=headers, data=body, verify=False) print(response.json()) 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes...it works now...but why need to dumps the body data? .... I have working with similar api before and use the same method... but this time..cannot...when dumps the data...resolved the issue. Do know why sir?
It how Python dictionary is sent as data to your API and what format the API expects. Dumping forces the dictionary to bytes in a way that is secured for sockets transmission
0
import requests url = 'https://api.wallet.com/withdraw' headers = { 'Authorization': 'Bearer EQD2_4d91M4TVbEBVyBF8J1UwpMJc361LKVCz6bBlffMW05o', 'Content-Type': 'application/json' } data = { 'amount': 'all' } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print('برداشت انجام شد!') else: print('خطا در برداشت وجود دارد.') 

1 Comment

IMO your answer might be better if you at least translated the messages in the print statements into English.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.