BUG解决:postman可以请求成功,但Python requests请求报403

目录

问题背景

问题定位

问题解决


问题背景

使用Python的requests库对接物联数据的接口之前一直正常运行,昨天突然请求不通了,通过进一步验证发现凡是使用代码调用接口就不通,而使用postman就能调通,请求参数啥的都没变。

接口返回的结果如下:

<!DOCTYPE html>\n<html>\n<head>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n<title>403</title>\n<style type="text/css">\nbody {\n background-color: #f3f3f4;\n color: #676a6c;\n height: 100%;\n font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";\n}\n\n.middle-box {\n max-width: 600px;\n z-index: 100;\n margin: 0 auto;\n padding-top: 40px;\n text-align: center;\n}\n\n.middle-box h1 {\n font-size: 170px;\n font-weight: 100;\n margin-top: 20px;\n margin-bottom: 10px;\n}\n\n.btn {\n border-radius: 3px;\n font-size: inherit;\n display: inline-block;\n width: 100px;\n height: 30px;\n line-height: 30px;\n background-color: #1ab394;\n border-color: #1ab394;\n color: #fff;\n cursor: pointer;\n font-size: 14px;\n}\n\n.btn:hover {\n background-color: #1c8570;\n}\n\n.btn:active {\n background-image: none;\n outline: 0;\n -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);\n box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);\n}\n\n.buttons {\n margin-top: 30px;\n}\n\n</style>\n</head>\n<body>\n <div class="middle-box">\n <h1>403</h1>\n <h3 class="font-bold">Forbidden</h3>\n <div class="error-desc">å\x9b\xa0æ\x9d\x83é\x99\x90é\x97®é¢\x98æ\x88\x96è¡\x8c为é\x9d\x9eæ³\x95ï¼\x8cæ\x82¨ç\x9a\x84访é\x97®è¢<<æ\x8b\x92ç>>\x9dã\x80\x82</div>\n <div class="buttons"><a class="btn btn-primary" οnclick="javascript:history.back()">è¿\x94å\x9b\x9e</a></div>\n </div>\n</body>\n</html>

问题定位

初步断定,在使用requests调用接口时少了点什么,而postman则有这个东西。

后来发现,postman在请求的时候自动加上了请求头User-Agent,而代码中headers中没有配置这一项。

User-Agent请求头是HTTP请求中的一个头部字段,它包含了发起请求的应用程序的信息。这个字段通常用于告知服务器,发起请求的是哪种浏览器、设备、操作系统或者应用程序。服务器可以根据这个信息来决定是否允许该请求,或者根据不同的客户端类型返回不同的内容。

User-Agent字符串通常包含了以下信息:

  • 浏览器名称和版本
  • 操作系统名称和版本
  • 设备类型(如智能手机、平板电脑、桌面电脑)
  • CPU类型
  • 默认语言

问题解决

在代码中加上自定义User-Agent请求头即可:

python 复制代码
import requests

url = "......"
headers = {'Content-Type': 'application/x-www-form-urlencoded', 
           'User-Agent': "PostmanRuntime/7.39.0"}
req = requests.get(url=url, headers=headers)

同理,也可以将User-Agent请求头设置为Chrome浏览器:

python 复制代码
headers = { 'Content-Type': 'application/x-www-form-urlencoded', 
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}

该请求头告诉服务器,请求是由一个运行在Windows 10上的64位版本的Chrome 58浏览器发起的。

相关推荐
@Aurora.17 小时前
【测试】BUG
bug
%d%d23 天前
undefined reference to CPUAllocatorSingleton::instance
bug
gxn_mmf5 天前
典籍知识问答重新生成和消息修改Bug修改
前端·bug
千里马学框架6 天前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
Bear on Toilet6 天前
Bug日记——实现“日期类”
开发语言·c++·bug
Direction_Wind6 天前
flinksql bug : Max aggregate function does not support type: CHAR
bug
mg6687 天前
Python Bug 修复案例分析:多线程数据竞争引发的bug 两种修复方法
bug
黎猫大侠8 天前
一次Android Fragment内存泄露的bug解决记录|Fragment not attach to an Activity
android·bug
七七小报8 天前
uniapp-商城-48-后台 分类数据添加修改弹窗bug
uni-app·bug
windwind20008 天前
发行基础:本地化BUG导致审核失败
游戏·青少年编程·编辑器·bug·创业创新·玩游戏