简体   繁体   中英

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

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.

我相信你的DATA变量应该是一个字典而不是一个字符串?

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

I had the same problem getting the response and I tried a whole bunch of things. By it turned out to be the problem with the data format. The response was null (with status code 200) when I send the dictionary directly. When I send a json string as data, I am getting the response as expected.

import 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=json.dumps(DATA), headers=HEADERS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM