美文网首页软件测试
Python: 模拟post请求时的相关问题:cookie,js

Python: 模拟post请求时的相关问题:cookie,js

作者: Dspark | 来源:发表于2019-12-02 16:25 被阅读0次

需求:模拟客户端进行post请求,除业务参数外,还需带有sign参数
在实现该需求时,使用了 requests.request()方法: requests.request('POST',url,data=rdata,cookies=rcookie),随后执行时遇到了以下问题:

1. <Response 403>

原因:缺失了header相关信息
解决方法:在header中添加 User-Agent 和 refer等相关信息

   headers = {
      'User-Agent': '...',
      'referer': '....'
    }
2. 提示 参数不合法

原因:请求的body中,需以 json 形式传参,而初始调用时使用的为data
解决方法:解决方法有俩,
① 因为request() 参数中本来即存在 json,可直接使用json来传参,即:
requests.request('POST',url,json=json.dumps(rdata),cookies=rcookie)
② 也可继续使用 data 参数,此时需指定 content-type:

   headers = {'Content-Type': 'application/json'}
   req = requests.request('POST',url,headers=headers,data=json.dumps(rdata),cookies=rcookie)

P.S. 若不指定content-type,data为dict时,默认为application/x-www-form-urlencoded;
data为str时,则默认为application/json。

相关文章

  • Python: 模拟post请求时的相关问题:cookie,js

    需求:模拟客户端进行post请求,除业务参数外,还需带有sign参数在实现该需求时,使用了 requests.re...

  • python调用js代码---PyV8

    背景:爬某网站数据。需要携带参数发POST请求,参数在网页js中,js太复杂,不便用python模拟。所以直接用p...

  • moco带cookie和headers请求

    moco可以接收带模拟cookie的请求,也可以模拟返回携带cookie的请求添加cookie和headers请求...

  • SpringBoot开发接口

    1、模拟get请求2、模拟get请求返回cookie3、模拟get请求携带cookie信息4、模拟get请求携带参...

  • Postman接口测试

    1. post、get接口测试 get请求 post请求 2.带cookie的请求测试 带cookie的请求 1....

  • iOS请求方法和网络安全

    GET和POST请求 GET和POST请求简介 GET请求模拟登陆 POST请求模拟登陆 GET和POST的对比 ...

  • 爬虫(3)--- 一起来爬知乎

    目录 1、模拟登陆1.1 何为Cookie1.2 登陆分析1.2.1 解析POST请求1.2.2 模拟登陆流程1....

  • iOS请求方法和网络安全

    GET和POST请求GET和POST请求简介GET请求模拟登陆POST请求模拟登陆GET和POST的对比保存用户信...

  • 命令行curl模拟get post请求

    新学到一个小技巧,在命令行中模拟get、post请求 模拟post请求 模拟post请求,在命令行中输入:-d代表...

  • 六、post请求

    POST请求相比较GET请求,POST请求比较复杂。因为 Node.js认为,使用POST请求时,数据量会比较多。...

网友评论

    本文标题:Python: 模拟post请求时的相关问题:cookie,js

    本文链接:https://www.haomeiwen.com/subject/tdqfgctx.html