简体   繁体   中英

Error in POST API python while trying to send data

Here is the json data to send

{
  "mobile": "1234567890"
}

Here is the python code:

from urllib import response
import requests
import urllib 
import json 

query = {"mobile": "1234567890"}
headers =  {"Content-Type":"application/json"}
url = 'https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP'
y = requests.post(url, data=query, headers=headers)

print(y)

Response from the API:

c:/pythonprojects/covid-otp-requests.py
<Response [400]>

I am a newbie, I don't understand the mistake I am making, Can someone please help me out?.

Fix here:

from urllib import response
import requests
import urllib 
import json 

params = {
    "mobile": "1234567890"
}
headers =  {"Content-Type":"application/json"}
response = requests.post("https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP", data=json.dumps(params))
print(response)

details = response.json()

print(details)

The parameters has to be sent in json format which I making a mistake in.

Thank you all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM