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.
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())