Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests.post() returns None

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().

like image 759
Thomas Z Avatar asked Nov 01 '25 20:11

Thomas Z


1 Answers

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

like image 124
MrName Avatar answered Nov 03 '25 09:11

MrName



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!