【发布时间】:2020-05-12 23:20:45
【问题描述】:
我正在使用 pandas 和 requests 创建一个发布请求,而我创建的那个发给我一个状态码 200 而不是 201。
在这篇文章中,我从一个数据帧发送一个 JSON。这部分似乎很好。 不知道header好不好,里面改了很多东西,都没有成功。
这个问题没有显示任何错误,受请求影响的服务器也没有给出任何迹象。 第一个请求给了我访问令牌并且运行良好。
def post_json(nbr_requests):
auth_json = {'grant_type': 'password', 'client_id': 'hidden','client_secret':'hidden','username':'hidden','password':'hidden'}
auth_response = requests.post('http://hidden:8080/lot/of/stufs/token',data=auth_json)
token = auth_response.json()["access_token"]
api_call_headers = {'content-type':'application/json', 'accept':'application/json','authorization': 'Bearer' + token}
url_to_go = "http://localhost:8080/hidden/link"
for i in range(nbr_requests):
api_call_response = requests.post(url_to_go, headers=api_call_headers, json=json_array_to_send[i],data={"key": "value"})
print (api_call_response.status_code)
【问题讨论】:
-
您向我们展示了客户端代码,但询问了服务器。向我们展示服务器代码,即相关端点。无论如何,
200是默认的“ok”代码。如果你想返回其他任何东西,你必须明确地这样做。 -
由于机密问题,我无法显示服务器端代码,但使用 Postman,我没有此状态代码问题,并且我得到了应有的 201。使用代码 201,它们是数据库中的插入,我至少可以看到结果。
-
然后创建一个minimal reproducible example。如果没有看到任何代码,我们将无法帮助您。
标签: python json request http-error