post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种)
application/x-www-form-urlencoded(表单形式)
application/json(body为json格式)
text/xml(body为xml格式)
multipart/form-data(上传文件)
表单参数形式的post请求:用data传参
fiddler请求响应内容;
代码:
import requests import json #form表单形式的post请求,用data传参,Content-Type: application/x-www-form-urlencoded payload={"username":"admin","password":"admin","validateCode":"","rememberMe":"false"} response3=requests.post("http://www.luckyframe.cn/login",data=payload) print("响应content:",response3.content) print("响应text:",response3.text) #反序列化,字符串转换为对象,这里是字典 loginresponse=json.loads(response3.text) print(loginresponse) #根据字典方法取key值 print(loginresponse['msg']) print(loginresponse['code']) #requests内置json方法,把json字符串转换为json对象 print(response3.json()) loginresponse1=response3.json() #根据字典方法取key值 print(loginresponse1['msg']) print(loginresponse1['code']) print('响应码:',response3.status_code) print('响应头:',response3.headers) print('cookie:',response3.cookies) print('响应编码:',response3.encoding) print('请求url:',response3.url) print('重定向的来源url:',response3.history) print(response3.reason) print(response3.iter_content())
运行结果:
D:Python37python.exe E:/PycharmProjects/home_UI/apistudy/requestsstudy.py 响应content: b'{"msg":"xe6x93x8dxe4xbdx9cxe6x88x90xe5x8ax9f","code":0}' 响应text: {"msg":"操作成功","code":0} {'msg': '操作成功', 'code': 0} 操作成功 0 {'msg': '操作成功', 'code': 0} 操作成功 0 响应码: 200 响应头: {'Set-Cookie': 'JSESSIONID=eb526590-bf5d-42b8-8c44-db4335575fd4; Path=/; HttpOnly, rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Fri, 12-Jun-2020 14:25:56 GMT', 'Content-Type': 'application/json;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Sat, 13 Jun 2020 14:25:56 GMT'} cookie: <RequestsCookieJar[<Cookie JSESSIONID=eb526590-bf5d-42b8-8c44-db4335575fd4 for www.luckyframe.cn/>]> 响应编码: UTF-8 请求url: http://www.luckyframe.cn/login 重定向的来源url: [] <generator object iter_slices at 0x000001E0FE0C3548>