接口请求构造

1、请求目标构造

import requests

r = requests.get('https://api.github.com/events')

2、请求参数构造

     (1) get query:path、query
     (2) post body:
           form:表单
           结构化请求:json、xml、json rpc
           binary:二进制

3、Get Query请求

payload = {'key1':'value1','key2':'value2'}

r = requests.get('https://httpbin.org/get',params=payload)

4、Form请求参数构造

payload = {'key1':'value1','key2':'value2'}

r = requests.post('https://httpbin.org/post',data=payload)

5、文件上传

files = {'file':open('report.xls','rb')}

r = requests.post(url,files=files)

6、header构造

     普通的header:

headers = {'useragent':'my-app/0.0.1'}

r = requests.get(url,headers=headers)

     cookie:

cookies = dict(cookies_are='working')

r = requests.get(url,cookies =cookies )

7、响应结果

     (1) 基本信息:r.url、r.status_code、r.headers、
     (2) 响应结果:
             r.text=r.encoding + r.content
             r.json()=r.encoding + r.content + content type json
             r.raw.read(10)
     (3) 对应的请求内容:r.request

你可能感兴趣的:(接口测试,post,url)