可以下载本人编写的接口源码配合示例使用:
点击下载压缩包,压缩包内含有接口源码及使用说明,不需要额外安装requests
关于 response
一个典型的 HTTP 响应由以下几个部分组成:
状态行
HTTP/1.1 200 OK
HTTP版本:例如 HTTP/1.1
状态码:用于表示请求的处理结果。例如 200 表示请求成功,404 表示资源未找到。
状态描述:对状态码的简要描述,例如 OK。
常见的状态码:
200 OK:请求成功。
201 Created:资源已成功创建(通常是POST请求的响应)。
204 No Content:请求成功,但没有响应体内容。
400 Bad Request:请求无效,通常是由于请求参数有误。
401 Unauthorized:未授权,需进行身份验证。
403 Forbidden:服务器拒绝请求,权限不足。
404 Not Found:请求的资源未找到。
500 Internal Server Error:服务器内部错误。
响应头
Content-Type: application/json
Content-Length: 123
Date: Mon, 27 Sep 2024 14:22:30 GMT
Content-Type: 指定响应内容的MIME类型,例如 text/html, application/json
Content-Length: 响应体的长度,单位为字节
Date: 响应生成的时间
其他的响应头:如:Set-Cookie、Server、Connection等
空行
头部和响应体之间的空行,表示头部的结束。
响应体
{
'blog address': 'www.jzy-blogs.cn',
'message': {
'Content-type': 'application/soap+xml; charset=utf-8',
'data': None
},
'method': 'POST'
}
响应体是服务器返回的实际数据,通常是 HTML、JSON、XML 等格式的内容。对于成功的请求,响应体是请求结果的具体内容。
完整的响应
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 123
Date: Mon, 27 Sep 2024 14:22:30 GMT
{
'blog address': 'www.jzy-blogs.cn',
'message': {
'Content-type': 'application/soap+xml; charset=utf-8',
'data': None
},
'method': 'POST'
}
状态行
import requests
# 定义请求的URL。该地址是部署在本地的接口地址,读者可以下载源码使用
url = 'http://127.0.0.1:5000/Response_list/'
# 发起请求,并将响应赋值给response变量
response = requests.get(url)
# 打印响应的状态行信息
print('响应状态行信息:')
print(f'HTTP版本:{response.raw.version}') # 11 表示 HTTP/1.1, 10 表示 HTTP/1.0
print(f'Code码:{response.status_code} {response.raw.status}') # 两种方式都可以获取code
print(f'状态描述: {response.raw.reason}') # 获取状态描述
print(f'Code码为2xx:{response.ok}') # ok方法可以判断响应的code是否为2xx,是返回True,不是返回False
运行结果
响应状态行信息:
HTTP版本:11
Code码:200 200
状态描述: OK
Code码为2xx:True
响应头部
from pprint import pprint
import requests
# 定义请求的URL。该地址是部署在本地的接口地址,读者可以下载源码使用
url = 'http://127.0.0.1:5000/Response_headers/'
# 发起请求,并将响应赋值给response变量
response = requests.get(url)
# 打印响应的头信息
print('打印完整的头信息:')
# response.headers属性返回的是一个类似字典的对象,但并不完全是字典。转换为dict并使用pprint,可以美观输出
pprint(dict(response.headers))
print('打印Cookies信息:')
print(response.headers['Set-Cookie'])
print('打印Cookies信息中token值:')
print(response.headers['Set-Cookie'].split(';')[0].split('=')[1]) # split方法按照特定字符分割字符串,返回列表
运行结果
打印完整的头信息:
{'Connection': 'close',
'Content-Length': '60',
'Content-Type': 'application/json',
'Date': 'Tue, 24 Sep 2024 06:29:23 GMT',
'Server': 'Werkzeug/3.0.4 Python/3.12.5',
'Set-Cookie': 'token=bx7qz8ECXo37im8th03e; Expires=Tue, 01 Oct 2024 06:29:23 '
'GMT; Max-Age=604800; Path=/'}
打印Cookies信息:
token=bx7qz8ECXo37im8th03e; Expires=Tue, 01 Oct 2024 06:29:23 GMT; Max-Age=604800; Path=/
打印Cookies信息中token值:
bx7qz8ECXo37im8th03e
其它头信息可以像 Set-Cookie
一样进行解析。
响应体
from pprint import pprint
import requests
# 定义请求的URL。该地址是部署在本地的接口地址,读者可以下载源码使用
url = 'http://127.0.0.1:5000/Response_json/'
# 发起请求,并将响应赋值给response变量
response = requests.get(url)
print('打印响应体信息:')
print(response.text) # 任何响应体信息都可以使用此方法获取,返回类型为str
pprint(response.json()) # 响应体是application/json类型,可以使用此方法,返回类型为dict
运行结果
打印响应体信息:
{"blog address":"www.jzy-blogs.cn","message":{"code":"","detail":"","message":"\u6210\u529f","result":{"list":[{"alerts":null,"business_config_id":2,"business_config_name":"\u6d4b\u8bd5\u68c0\u9a8c\u9879\u914d\u7f6e","cllx":"K43","cllx_translated":"\u5fae\u578b\u8f7f\u8f66","clpp1":"\u6bd4\u4e9a\u8fea\u724c","clsbdh":"LGXC14AA2A1052348","clxh":"QCJ7100L","created_at":0,"finish_at":1727163415,"gcjk":"A","gcjk_translated":"\u56fd\u4ea7","hphm":"\u8499K935N5","id":16157,"jyjgbh":"111001","jylb":"01","jylb_translated":"\u5728\u7528\u8f66\u68c0\u9a8c","lsh":"54808343617047142188","manual_re_verify_at":0,"manual_re_verify_by":0,"manual_re_verify_mark":false,"modified":false,"organization_name":"\u68c0\u9a8c\u673a\u6784","random_re_verify_at":0,"random_re_verify_by":0,"random_re_verify_mark":false,"rlzl":"A","rlzl_translated":"\u6c7d\u6cb9","start_at":1727163415,"status":9,"syr":"\u4ef2\u6893\u6587","syxz":"A","syxz_translated":"\u975e\u8425\u8fd0","updated_at":0,"verify_result":1,"verify_time_in_seconds":1727163415,"verify_type":1},{"alerts":null,"business_config_id":2,"business_config_name":"\u6d4b\u8bd5\u68c0\u9a8c\u9879\u914d\u7f6e","cllx":"K43","cllx_translated":"\u5fae\u578b\u8f7f\u8f66","clpp1":"\u6bd4\u4e9a\u8fea\u724c","clsbdh":"LGXC14AA2A1052348","clxh":"QCJ7100L","created_at":0,"finish_at":1727163415,"gcjk":"A","gcjk_translated":"\u56fd\u4ea7","hphm":"\u8499K935N5","id":16156,"jyjgbh":"111001","jylb":"01","jylb_translated":"\u5728\u7528\u8f66\u68c0\u9a8c","lsh":"46848464982678957544","manual_re_verify_at":0,"manual_re_verify_by":0,"manual_re_verify_mark":false,"modified":false,"organization_name":"\u68c0\u9a8c\u673a\u6784","random_re_verify_at":0,"random_re_verify_by":0,"random_re_verify_mark":false,"rlzl":"A","rlzl_translated":"\u6c7d\u6cb9","start_at":1727163415,"status":9,"syr":"\u4ef2\u6893\u6587","syxz":"A","syxz_translated":"\u975e\u8425\u8fd0","updated_at":0,"verify_result":1,"verify_time_in_seconds":1727163415,"verify_type":1},{"alerts":null,"business_config_id":2,"business_config_name":"\u6d4b\u8bd5\u68c0\u9a8c\u9879\u914d\u7f6e","cllx":"K43","cllx_translated":"\u5fae\u578b\u8f7f\u8f66","clpp1":"\u6bd4\u4e9a\u8fea\u724c","clsbdh":"LGXC14AA2A1052348","clxh":"QCJ7100L","created_at":0,"finish_at":1727163415,"gcjk":"A","gcjk_translated":"\u56fd\u4ea7","hphm":"\u8499K935N5","id":16155,"jyjgbh":"111001","jylb":"01","jylb_translated":"\u5728\u7528\u8f66\u68c0\u9a8c","lsh":"92221529187043778122","manual_re_verify_at":0,"manual_re_verify_by":0,"manual_re_verify_mark":false,"modified":false,"organization_name":"\u68c0\u9a8c\u673a\u6784","random_re_verify_at":0,"random_re_verify_by":0,"random_re_verify_mark":false,"rlzl":"A","rlzl_translated":"\u6c7d\u6cb9","start_at":1727163415,"status":9,"syr":"\u4ef2\u6893\u6587","syxz":"A","syxz_translated":"\u975e\u8425\u8fd0","updated_at":0,"verify_result":1,"verify_time_in_seconds":1727163415,"verify_type":1}],"page_count":3,"page_index":1,"page_size":10,"total_count":26},"success":true},"method":"GET"}
{'blog address': 'www.jzy-blogs.cn',
'message': {'code': '',
'detail': '',
'message': '成功',
'result': {'list': [{'alerts': None,
'business_config_id': 2,
'business_config_name': '测试检验项配置',
'cllx': 'K43',
'cllx_translated': '微型轿车',
'clpp1': '比亚迪牌',
'clsbdh': 'LGXC14AA2A1052348',
'clxh': 'QCJ7100L',
'created_at': 0,
'finish_at': 1727163415,
'gcjk': 'A',
'gcjk_translated': '国产',
'hphm': '蒙K935N5',
'id': 16157,
'jyjgbh': '111001',
'jylb': '01',
'jylb_translated': '在用车检验',
'lsh': '54808343617047142188',
'manual_re_verify_at': 0,
'manual_re_verify_by': 0,
'manual_re_verify_mark': False,
'modified': False,
'organization_name': '检验机构',
'random_re_verify_at': 0,
'random_re_verify_by': 0,
'random_re_verify_mark': False,
'rlzl': 'A',
'rlzl_translated': '汽油',
'start_at': 1727163415,
'status': 9,
'syr': '仲梓文',
'syxz': 'A',
'syxz_translated': '非营运',
'updated_at': 0,
'verify_result': 1,
'verify_time_in_seconds': 1727163415,
'verify_type': 1},
{'alerts': None,
'business_config_id': 2,
'business_config_name': '测试检验项配置',
'cllx': 'K43',
'cllx_translated': '微型轿车',
'clpp1': '比亚迪牌',
'clsbdh': 'LGXC14AA2A1052348',
'clxh': 'QCJ7100L',
'created_at': 0,
'finish_at': 1727163415,
'gcjk': 'A',
'gcjk_translated': '国产',
'hphm': '蒙K935N5',
'id': 16156,
'jyjgbh': '111001',
'jylb': '01',
'jylb_translated': '在用车检验',
'lsh': '46848464982678957544',
'manual_re_verify_at': 0,
'manual_re_verify_by': 0,
'manual_re_verify_mark': False,
'modified': False,
'organization_name': '检验机构',
'random_re_verify_at': 0,
'random_re_verify_by': 0,
'random_re_verify_mark': False,
'rlzl': 'A',
'rlzl_translated': '汽油',
'start_at': 1727163415,
'status': 9,
'syr': '仲梓文',
'syxz': 'A',
'syxz_translated': '非营运',
'updated_at': 0,
'verify_result': 1,
'verify_time_in_seconds': 1727163415,
'verify_type': 1},
{'alerts': None,
'business_config_id': 2,
'business_config_name': '测试检验项配置',
'cllx': 'K43',
'cllx_translated': '微型轿车',
'clpp1': '比亚迪牌',
'clsbdh': 'LGXC14AA2A1052348',
'clxh': 'QCJ7100L',
'created_at': 0,
'finish_at': 1727163415,
'gcjk': 'A',
'gcjk_translated': '国产',
'hphm': '蒙K935N5',
'id': 16155,
'jyjgbh': '111001',
'jylb': '01',
'jylb_translated': '在用车检验',
'lsh': '92221529187043778122',
'manual_re_verify_at': 0,
'manual_re_verify_by': 0,
'manual_re_verify_mark': False,
'modified': False,
'organization_name': '检验机构',
'random_re_verify_at': 0,
'random_re_verify_by': 0,
'random_re_verify_mark': False,
'rlzl': 'A',
'rlzl_translated': '汽油',
'start_at': 1727163415,
'status': 9,
'syr': '仲梓文',
'syxz': 'A',
'syxz_translated': '非营运',
'updated_at': 0,
'verify_result': 1,
'verify_time_in_seconds': 1727163415,
'verify_type': 1}],
'page_count': 3,
'page_index': 1,
'page_size': 10,
'total_count': 26},
'success': True},
'method': 'GET'}
确定响应的类型为 application/json
时,就可使用 response.json()
,响应的类型不是 application/json
时,就使用 response.text
。然后按照需要去解析响应数据。
THEEND
© 转载需要保留原始链接,未经明确许可,禁止商业使用。CC BY-NC-ND 4.0
...