Reputation: 565
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.
Upvotes: 4
Views: 13993
Reputation: 1
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('خطا در برداشت وجود دارد.')
Upvotes: 0
Reputation: 15588
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())
Upvotes: 8