如何从Python的POST请求中获取响应数据

3

我有一个正在运行的openapi3服务器,我正在尝试进行认证。

我可以使用curl轻松完成它。

curl -k -X POST "https://localhost:8787/api/login/user" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{\"username\":\"admin\",\"password\":\"admin\"}"

#and i get the token back
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNjEyODY3NDU3LCJleHAiOjE2MTI4NzEwNTd9.9eFgomcpJbinN7L4X1VOHfZGvJeUvHiv6WPjslba1To

但是当我使用python时,我只得到了响应代码(200),没有包括令牌在内的数据。

以下是我的python脚本:

import requests
import os

url = str(os.environ.get('API_SERVER_URL'))+'login/user'
head = {'accept': 'application/json', 'Content-Type': 'application/json'}
data = {"username": "admin", "password": "admin"}             }
response = requests.post(url, json=data, headers=head, verify=False)

print(response)


所有我得到的是200响应。
<Response [200]>

1个回答

4

你想获取响应内容吗?

例如:

print(response.text)

如果您期望使用 JSON

print(response.json())

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接