"""
@author:hyongchang
@file:test_case_03.py
@time:2020/06/30
"""
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
import requests
Host = "http://127.0.0.1"
add_course_url = f"{Host}/api/mgr/sq_mgr/"
headers = {
"Content-Type" : "application/x-www-form-urlencoded"
}
payload = {
"action" : "add_course",
"data" : """{
"name":"初中化学",
"desc":"周四 16:00-18:00",
"display_idx":"4"
}"""
}
response = requests.post(add_course_url, data=payload)
print(response.request.headers)
print(response.text)
print(response.request.body)
"""
输出:
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '288', 'Content-Type': 'application/x-www-form-urlencoded'}
{"retcode": 0, "id": 2175}
action=add_course&data={
++++++++++++++++++++++"name":"初中化学",
++++++++++++++++++++++"desc":"初中化学课程",
++++++++++++++++++++++"display_idx":"4"
++++++++++++++++++++}
总结:当接口发送post请求时,headers值不指定Content-Type:
1、如果传入的参数data为dict,则请求头中Content-Type的值为默认为表单格式:application/x-www-form-urlencoded
2、如果传入的参数data为str,则请求头中Content-Type的值为text/plain
使用data参数提交数据时,请求体request.body的内容为a=1&b=2这种形式
"""
response = requests.post(add_course_url, data=payload)
print(response.request.headers)
print(response.text)
print(response.request.body)
"""
输出:
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '288', 'Content-Type': 'application/x-www-form-urlencoded'}
{"retcode": 2, "reason": "\u540c\u540d\u8bfe\u7a0b\u5b58\u5728"}
action=add_course&data={
++++++++++++++++++++++"name":"初中化学",
++++++++++++++++++++++"desc":"初中化学课程",
++++++++++++++++++++++"display_idx":"4"
++++++++++++++++++++}
总结:上述响应输出内容"\\u",说明输出的响应内容的字符编码为Unicode编码,要将编码进行转换,可在获取响应内容前添加response.encoding = "unicode_escape"
"""
add_couse_json_url = f"{Host}/apijson/mgr/sq_mgr/"
headers = {
"Content-Type" : "application/x-www-form-urlencoded"
}
payload = {
"action" : "add_course",
"data" : """{
"name":"通信原理",
"desc":"周三 14:00-16:00",
"display_idx":"5"
}"""
}
response = requests.post(add_couse_json_url, json=payload)
print(response.request.headers)
response.encoding = "unicode_escape"
print(response.text)
print(response.request.body.decode("unicode_escape"))
"""
输出:
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '229', 'Content-Type': 'application/json'}
{"retcode": 0, "id": 2180}
b'{"action": "add_course", "data": "{\\n \\"name\\":\\"\\u901a\\u4fe1\\u539f\\u7406\\",\\n \\"desc\\":\\"\\u5468\\u4e09 14:00-16:00\\",\\n \\"display_idx\\":\\"5\\"\\n }"}'
总结:当接口发送post请求时,headers值不指定Content-Type:
1、如果传入的参数json为dict或者str,则请求头中Content-Type的值为默认为:application/json
使用json参数提交数据时,请求体request.body的内容为{"a":"1","b":"2"}这种格式
"""
response = requests.post(add_couse_json_url, json=payload)
print(response.request.headers)
response.encoding = "unicode_escape"
print(response.text)
print(response.request.body.decode("unicode_escape"))
"""
输出:
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '229', 'Content-Type': 'application/json'}
{"retcode": 2, "reason": "同名课程存在"}
{"action": "add_course", "data": "{
"name":"通信原理",
"desc":"周三 14:00-16:00",
"display_idx":"5"
}"}
总结:总结:上述请求体内容带有"\\u",说明输出的请求体内容的字符编码为Unicode编码,要将编码进行转换,可在获取请求体内容改为response.request.body.decode("unicode_escape")
"""
转载请注明原文地址:https://ipadbbs.8miu.com/read-4122.html