python接口自动化2-发送post请求

前言

发送post的请求参考例子很简单,实际遇到的情况却是很复杂的,首先第一个post请求肯定是登录了,但登录是最难处理的。登录问题解决了,后面都简单了。

一、查看官方文档

1.学习一个新的模块,其实不用去百度什么的,直接用help函数就能查看相关注释和案例内容。

>>import requests

>>help(requests)

2.查看python发送get和post请求的案例

 >>> import requests
       >>> r = requests.get('https://www.python.org')
       >>> r.status_code
       200
       >>> 'Python is a programming language' in r.content
       True
    
    ... or POST:
    
       >>> payload = dict(key1='value1', key2='value2')
       >>> r = requests.post('http://httpbin.org/post', data=payload)
       >>> print(r.text)
       {
         ...
         "form": {
           "key2": "value2",
           "key1": "value1"
         },
         ...
       }

二、发送post请求

1.用上面给的案例,做个简单修改,发个post请求

2.payload参数是字典类型,传到如下图的form里

三、json

1.post的body是json类型,也可以用json参数传入。

2.先导入json模块,用dumps方法转化成json格式。

3.返回结果,传到data里

四、headers

1.以禅道登录为例,模拟登陆,这里需添加请求头headers,可以用fiddler抓包

2.讲请求头写成字典格式

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
            "Accept-Encoding": "gzip, deflate, br",
            "Content-Type": "application/json; charset=utf-8",
            "X-Requested-With": "XMLHttpRequest",
            "Cookie": "xxx.............",    # 此处cookie省略了
            "Connection": "keep-alive"
            }

五、禅道登录参考代码

 1 # coding:utf-8
 2 # coding:utf-8
 3 import requests
 4 # 禅道host地址
 5 host = "http://127.0.0.1"
 6 
 7 def login(s,username,psw):
 8     url = host+"/zentao/user-login.html"
 9 
10     h = {
11         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
12         "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
13         "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
14         "Accept-Encoding": "gzip, deflate",
15         "Referer": host+"/zentao/user-login.html",
16         # "Cookie":  # 头部没登录前不用传cookie,因为这里cookie就是保持登录的
17         "Connection": "keep-alive",
18         "Content-Type": "application/x-www-form-urlencoded",
19         }
20 
21     body1 = {"account": username,
22              "password": psw,
23              "keepLogin[]": "on",
24              "referer":  host+"/zentao/my/"
25             }
26 
27     # s = requests.session()   不要写死session
28 
29     r1 = s.post(url, data=body1, headers=h)
30     # return r1.content  # python2的return这个
31     return r1.content.decode("utf-8")  # python3
32 
33 def is_login_sucess(res):
34         if "登录失败,请检查您的用户名或密码是否填写正确。" in res:
35                 return False
36         elif "parent.location=" in res:
37                 return True
38         else:
39                 return False
40 
41 if __name__ == "__main__":
42     s = requests.session()
43     a = login(s, "admin", "e10adc3949ba59abbe56e057f20f883e")
44     result = is_login_sucess(a)
45     print("测试结果:%s"%result)
46 
47  

猜你喜欢

转载自www.cnblogs.com/jason89/p/9027687.html
今日推荐