使用requests库测试post请求 操作流程

发布时间:2024年01月11日

第一步 谷歌f12或其他抓包工具抓包,这里随机抓一个post请求

url:https://eva2.csdn.net/v3/06981375190026432f77c01bfca33e32/lts/groups/dadde766-b087-42da-8e67-d2499a520ee7/streams/a0119567-bf91-4314-ab75-f683ba6c0c0a/logs

第二步 导包

import requests

第三步 发起请求,注意:post请求和get请求的区别是post请求的参数不在url里,需要单独写进去

data={
  "labels": {},
  "logs": [
    {
      "contents": [
        {
          "pid": "blog",
          "ref": "https://blog.csdn.net/rank/list/total?spm=1001.2014.3001.5476",
          "curl": "https://blog.csdn.net/rank/list/force?type=city",
          "spm": "1001.2227",
          "extra": "{\"x\":795,\"y\":312,\"ev\":\"move\"}",
          "tos": 4,
          "adb": 0,
          "cCookie": "c_ins_um=-;c_ins_prid=1702092346856_873405;c_ins_rid=1702790946379_310048;c_ins_fref=https://mp.csdn.net/mp_blog/creation/editor;c_ins_fpage=/?utm_source=636161750;c_segment=15;c_sid=87f4e0e9d7b903d94216fe2a2195a611;c_first_ref=www.baidu.com;c_first_page=https%3A//blog.csdn.net/weixin_55059461/article/details/127380811;c_session_id=10_1704892269947.297931;c_dsid=11_1704892271191.714141;c_pref=https%3A//blog.csdn.net/2301_76297780%3Ftype%3Dcollect;c_ref=https%3A//blog.csdn.net/rank/list/total%3Fspm%3D1001.2014.3001.5476;c_page_id=default;",
          "t": 1704893667,
          "screen": "1536*864",
          "urn": "1704893373051-f51737c2-d6ea-48e6-aa0e-f21ef8641102",
          "vType": "U010000",
          "log_id": "1437",
          "sign": "48ed48fb80cd8e215e7297d9271ab5c4",
          "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
          "cid": "10_7173435430-1674400143644-553823",
          "uid": "2301_76297780",
          "sid": "10_1704892269947.297931",
          "dc_sid": "87f4e0e9d7b903d94216fe2a2195a611",
          "did": "10_7173435430-1674400143644-553823",
          "utm": "",
          "un": "2301_76297780",
          "__client_time__": 1704893666769
        }
      ]
    }
  ]
}
    url1="https://eva2.csdn.net/v3/06981375190026432f77c01bfca33e32/lts/groups/dadde766-b087-42da-8e67-d2499a520ee7/streams/a0119567-bf91-4314-ab75-f683ba6c0c0a/logs"
    my_post=requests.post(url1,data=data)

参数的查看和复制方法如图:

第四步 打印请求结果

print(f'状态码是:{my_post.status_code}')
print(f'响应报文是:{my_post.text}')
print(f'请求体是{my_post.headers}')

第五步 做断言(如果需要)

if my_post.status_code==200:
    print('请求成功')
else:
    print('请求失败')

代码运行结果

文章来源:https://blog.csdn.net/2301_76297780/article/details/135513837
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。