Search code examples
pythonpython-3.xpython-requestspython-requests-htmlpython-requests-json

python > requests module > post > headers not working


Why I'm getting a response like this? I tried to ask people on Discord and no one could explain.

>>>import requests
>>>api_url = "my_url"
>>>headers = {"Content-type": "application/json"}
>>>res = requests.post(api_url, "text to post", headers=headers)
>>>res.json()
{'message': '400: Bad Request', 'code': 0}

Solution

  • Try this:

    import requests
    import json
    
    api_url = "my_url"
    body = {'xxx': 'text to post'}
    headers = {"Content-type": "application/json"}
    res = requests.post(api_url, data=json.dumps(body), headers=headers)
    res.json()