Josh
Josh

Reputation: 33

How to send raw data in a POST request Python

Body I'm trying to send:

update_request={
        "id": "f07de0a44c2911ea8fb2bc764e10b970",
        "user": {
            "user": "3491574055045",
            "timestamp": "1640049459",
            "signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
            "code": "test"
        }
    }

This is my code right now:

url = "https://api.ordergroove.com/customer/update_customer"

headers = {
    'content-type': 'application/json'
}

body = """
    update_request={{
         "id": "f07de0a44c2911ea8fb2bc764e10b970",
         "user": {
             "timestamp": "1640049459",
             "signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
             "code": "test"
         }
     }}
"""

#Send and print response
response = requests.post(url, data=body, headers=headers)

If I run this in Postman though it works just fine: Postman screenshot

Upvotes: 2

Views: 7353

Answers (3)

PDHide
PDHide

Reputation: 19939

import requests

url = "https://46463d29-e52d-4bb9-bdda-68f0dfd7d06d.mock.pstmn.io/test"

payload = " update_request={{\r\n         \"id\": \"f07de0a44c2911ea8fb2bc764e10b970\",\r\n         \"user\": {\r\n             \"timestamp\": \"1640049459\",\r\n             \"signature\": \"YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D\",\r\n             \"code\": \"test\"\r\n         }\r\n     }}"
headers = {
  'Content-Type': 'text/plain'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

you can geenrate the code form postman itself

enter image description here

Upvotes: 4

Keegan Murphy
Keegan Murphy

Reputation: 832

In your case, your content-type specifies JSON, which is a common body type, so requests added a completely different way to send a json body (using the json parameter) by specifying json=body. Body is another dictionary type, which is then parsed into a string for you and sent with the request. Both x-www-form encoded and json are common body types that are essentially dictionaries, so they can be mixed up often but are NOT interchangeable.


data = {
"update_request":{
     "id": "f07de0a44c2911ea8fb2bc764e10b970",
     "user": {
         "timestamp": "1640049459",
         "signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
         "code": "test"
     }
   }
}

response = requests.post(url,json=data)#data=body for x-www-urlencoded form data, json=body for content-type json

Requests in this case will automatically add the content-type json header and format the dictionary into a string for you correctly, instead of sending the content-type x-www-form encoded content-type header if you were to put body=data

You are still able to use raw json data for requests by just passing in json=raw_data, raw data being a string. This is frowned upon since if there is any formatting issues, servers will likely not be able to read your request body. No reason to do that when you can pass in a python dictionary object as previously shown, and requests parses it into a string for you!

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113978

maybe ... and this is a big maybe

url = "https://api.ordergroove.com/customer/update_customer"


data = {"update_request":{
     "id": "f07de0a44c2911ea8fb2bc764e10b970",
     "user": {
         "timestamp": "1640049459",
         "signature": "YQvl1dWkN6MrHQ8xGwEQndVo2QdPSzc6EqLJslzNjy4%3D",
         "code": "test"
     }
   }
}

requests.post(url,json=data)

might work ...

Upvotes: 1

Related Questions