初学者API麻烦:400个状态代码,带有GitHub示例API
我正在练习与Python向API的请求。我目前正在使用GitHub上的此API练习: https://github.com/vdespa/introduction-to-postman-course/blob/main/simple-books-api.md.md 。
我想提出帖子请求以订购书。为了提出此请求,我提供了一个int book ID和str客户名称。但是,我得到的响应始终是“无效或丢失的bookid”。
但是,当我使用Postman提出相同的请求时,它可以使用真正的布尔值和STR订单ID响应。当我使用卷发和终端时,它也可以工作。
任何帮助将不胜感激!谢谢。 :)
import requests
import json
order_url = "https://simple-books-api.glitch.me/orders"
my_post_1 = {"bookId" : 1,
"customerName" : "Anna"}
header_1 = {"Authorization" : my_token}
post_text_1 = requests.post (order_url, data = my_post_1, headers = header_1)
print ("status:", post_text_1)
print ("respose:", post_text_1.text)
I am practicing making requests to APIs with Python. I am currently practicing with this API found on GitHub: https://github.com/vdespa/introduction-to-postman-course/blob/main/simple-books-api.md.
I would like to make a POST request to order a book. To make this request I provide an int book ID and a str customer name. However, the response I am getting is consistently "Invalid or missing bookId".
But when I use Postman to make this same request it works, responding with a True boolean and a str order ID. It also works when I use cURL and my terminal.
Any help would be appreciated! Thank you. :)
import requests
import json
order_url = "https://simple-books-api.glitch.me/orders"
my_post_1 = {"bookId" : 1,
"customerName" : "Anna"}
header_1 = {"Authorization" : my_token}
post_text_1 = requests.post (order_url, data = my_post_1, headers = header_1)
print ("status:", post_text_1)
print ("respose:", post_text_1.text)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当请求在Postman和/或浏览器中工作但不在请求中时,请检查标题。这通常是造成问题的原因。使标头相同,然后尝试,或者尝试添加更常见的标题。在这种情况下,似乎需要内容类型的标题。您应该将数据转储到JSON字符串中,或者尝试Buran在评论中的建议。以下对我有用:
When a request works in Postman and/or the browser but not in requests, check the headers. This is usually the cause of the problem. Make the headers the same and then try, or just try adding the more commonly required headers. In this case, the Content-Type header seems to be needed. And you should dump the data to a json string or try what Buran suggests in the comments. The following worked for me: