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().
I believe your DATA variable should be a dict as opposed to a string?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With