Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use "format=json&data=" in post requests when developing in python?

format=json&data={
  "pickup_location": {
    "pin": "110096",
    "add": "address",
    "phone": "1111111111",
    "state": "Delhi",
    "city": "Delhi",
    "country": "India",
    "name": "name of pickup/warehouse location registered with delhivery"
  }
}

The data above is the payload of the post required on the API document.

I don't know how to transfer this data because of "format=json&data=".

payload = {
    "pickup_location": {
        "pin": "110096",``
        "add": "Changsha",  # address of warehouse
        "phone": "1111111111",
        "state": "Delhi",
        "city": "Delhi",
        "country": "India"
    }
}

payload = 'format=json&data={}'.format(payload)
r = requests.post(url_test, json=payload, headers=headers)
payload = {
    'format': 'json',
    'data': {
        "pickup_location": {
            "pin": "110096",
            "add": "Changsha",  # address of warehouse
            "phone": "1111111111",
            "state": "Delhi",
           "city": "Delhi",
           "country": "India"
        }
    }
}

payload = 'format=json&data={}'.format(payload)
r = requests.post(url_test, json=payload, headers=headers)

These are the two pieces of code I've tried.

The end result is the same: "format key missing in POST".

I also looked it up on the Internet, but I couldn't find the right answer.

So I came to ask for help, 3Q.

like image 352
Peter Avatar asked Dec 15 '25 15:12

Peter


1 Answers

I had also same problem actually payload is raw data so do this

payload = {
"pickup_location": {
    "pin": "110096",``
    "add": "Changsha",  # address of warehouse
    "phone": "1111111111",
    "state": "Delhi",
    "city": "Delhi",
    "country": "India"
}
}

payload = f'format=json&data={json.dumps(payload,default=str,indent=4)}'
response=requests.post(url_order,data=payload,headers=headers)
like image 119
Rajat Avatar answered Dec 17 '25 09:12

Rajat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!