Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Requests payload format

Tags:

python

I am working on a POST request using requests library.

My post requests is working fine if I am using carriage returns in my payload, such as this:

payload = "{\r\n        \"name\": \r\n        {\r\n            \"@action\": \"login\",\r\n            \"@appname\": \"app\",\r\n            \"@class\": \"login\",\r\n            \"@nocookie\": 1,\r\n            \"@code\": \"101\",\r\n            \"@psw\": \"12345\",\r\n            \"@relogin\": \"0\",\r\n            \"@username\": \"user123\"\r\n        }\r\n}\r\n"

But if I format it to make the payload look pretty the request is not working:

payload = { 
    'name': 
        { 
            '@action': "login", 
            '@appname': "app", 
            '@class': "login", 
            'nocookie': 1, 
            '@code': "101", 
            'psw': "12345", 
            '@relogin': "0", 
            '@username': "user123" 
        } 
} 

I am getting 500 Error using the second payload. First payload works as expected. Any ideas?

like image 427
Shahboz Avatar asked Feb 13 '26 03:02

Shahboz


1 Answers

Most likely, you just need to create a JSON string from your structure using the function json.dumps first:

data = json.dumps(payload)

And then use the data variable instead of your original payload.

like image 161
David Ferenczy Rogožan Avatar answered Feb 15 '26 16:02

David Ferenczy Rogožan



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!