Search code examples
pythonjsonhttp-post

How to use JSON on POST in Python Requests


How I can use this JSON Data on POST

{"data":"{\"email\":\"anything65@gmail.com\",\"agreedToTerms\":true,\"profile\":{\"profileTypeID\":1,\"agreedToTerms\":true,\"firstName\":\"name\",\"lastName\":\"dfghjefws\",\"errors\":{}}}"}

This is My currently code, I got every time "message: Server Error"

data_profile2 = {"data":"{\"email\":\"anything65@gmail.com\",\"agreedToTerms\":true,\"profile\":{\"profileTypeID\":2,\"agreedToTerms\":true,\"firstName\":\"name\",\"lastName\":\"dfgh jefws\",\"errors\":{}}}"}
reqeust_1 = session.post(url_profile,headers=headers_profile, json=data_profile2)

Solution

  • requests doc

    POST request

    import requests
    
    payload= {"data": {"email": "anything65@gmail.com", "agreedToTerms": True , "profile": {"profileTypeID": 1,"agreedToTerms": True, "firstName": "name", "lastName": "dfghjefws", "errors": {}}}}
    request_1 = requests.get(url_profile, headers=headers_profile, data=payload)
    
    

    update

    If you use json=payload, your Content-Type in header will be application/json automatically. I assume that a session is not mandatory for that you want to do. Sessions persist parameters across many requests.