Python requests.post() returns None

11,926

Solution 1

I believe your DATA variable should be a dict as opposed to a string?

Solution 2

1) Trying out the same code with different endpoint works.

This means that the exact parameters sent in the Postman are not added in the client.

Try using a Apache tcpmon https://ws.apache.org/tcpmon/ to snoop the request from post man and the python client.

This will help you identify the missing parameters.

Solution 3

Thank your all for your quick support. This is the final version that works well!

import json
import requests

URL_PATH = "https://our.internal.server.rest.address"
HEADERS = {
    'Content-Type': 'application/json'
}

DATA = {
    "method"  : "object.read",
    "params"  : "",
    "id"      : 142
}

# R = requests.request("POST", URL_PATH, data=payload, headers=HEADERS)  # suggested by Postman
R = requests.post(URL_PATH, json=DATA, headers=HEADERS)

if R.ok:
    print("JSON: ", R.json())
else:
    R.raise_for_status()
Share:
11,926

Related videos on Youtube

Thomas Z
Author by

Thomas Z

Updated on June 04, 2022

Comments

  • Thomas Z
    Thomas Z over 1 year

    I know that this issue was discussed earlier but I am unable to find a working solution that fits my use case.

    We have an internal server (sorry, no external address available) that returns data. Invoking a POST Method on the Endpoint returns a JSON. I tried the post with the Postman tool to check a valid response is received. When using a Postman I receive the response as expected. An authentication is not necessary.

    However, Python Client fails to return data and an empty response is received.

    code snippet :

    import json
    import requests
    
    URL_PATH = "https://our.internal.server.rest.address"
    HEADERS = {
        'Content-Type': 'application/json'
    }
    
    DATA = '''{
        "method"  : "object.read",
        "params"  : "",
        "id"      : 142
    }'''
    
    S = requests.session()
    R = S.post(URL_PATH, headers=HEADERS, json=DATA)
    
    if R.ok:
        print("Type: ", R.headers["Content-Type"])
        print("Text: ", str(R.text))
        print("JSON: ", R.json())
        print("Content", R.content)
    else:
        R.raise_for_status()
    
    S.close()
    

    The output is:

    Type: application/json
    Text: null
    JSON: None
    Content b'null'

    Any idea what I'm doing wrong with my Python code and why there is no data returned, but is when using a separate tool?

    I also tried to use data= in the post():

    S.post(URL_PATH, headers=HEADERS, data=json.dumps(DATA))
    

    or skip the Session() by directly using requests.post().

    • Axel Persinger
      Axel Persinger almost 6 years
      Are you able to send any requests at all? Try a simple GET request to another URI, do you get a proper response?
    • Arount
      Arount almost 6 years
      Not sure but Postman surely allow you to export your query as curl command. Could you post it here? So we can check if there is any difference
    • Thomas Z
      Thomas Z almost 6 years
      That was a great suggestion! Yes, I found the menu option extract the request as code for Python. And know what? Now it works! No idea what exactly the error was. Postman sends the request via response = requests.request("POST", url, data=payload, headers=headers) but requests.post() worked as well. I'll drop the working snipped below...
  • matth
    matth almost 6 years
    So the concrete advice is to remove the two ''' from your DATA assignment.