requests 接口测试响应对象分析

requests API 演示demo 示例
import requests

url = 'https://ddsf.fangdd.com/ioux/loginByPassword'
headers = {"content-type": "application/json"}
data = { "username": "18688886666", "password": "Cwj123.."}
res = requests.post(url=url, headers=headers, json=data)
解析响应对象
print(dir(res))

# 输出结果
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate_
_', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr
__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection
', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_f
or_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
常用响应对象属性及类型
print("text:", res.text)
print("content:", res.content)
print("json:", res.json())   # 接口测试常用于断言对象
  1. text 属性
text: {"code":200,"data":{"userId":6859485,"userName":"测试陈柏霖","realName":"测试陈柏霖","mobile":"18688886666","email":"chenbolin@163.com","profileUrl":null,"roleId":1,"roleNam
e":"新房商服","orgId":3,"orgName":"测试自营分公司","serviceAgentCount":0,"cities":[{"cityName":"深圳","cityId":1337},{"cityName":"白山","cityId":10204},{"cityName":"北京","cityId"
:2179},{"cityName":"上海","cityId":121}],"isLogin":true,"isAs":false,"isPlatformAs":false,"dep":{"depName":"测试自营分公司","level":1,"parentDepId":null,"orgId":null,"depId":37,"p
ositionType":2},"staffManagerLevel":0,"orgs":null}}
  1. content 属性
content: b'{"code":200,"data":{"userId":6859485,"userName":"\xe6\xb5\x8b\xe8\xaf\x95\xe9\x99\x88\xe6\x9f\x8f\xe9\x9c\x96","realName":"\xe6\xb5\x8b\xe8\xaf\x95\xe9\x99\x88\xe6\x9f\
x8f\xe9\x9c\x96","mobile":"18688886666","email":"chenbolin@163.com","profileUrl":null,"roleId":1,"roleName":"\xe6\x96\xb0\xe6\x88\xbf\xe5\x95\x86\xe6\x9c\x8d","orgId":3,"orgName":
"\xe6\xb5\x8b\xe8\xaf\x95\xe8\x87\xaa\xe8\x90\xa5\xe5\x88\x86\xe5\x85\xac\xe5\x8f\xb8","serviceAgentCount":0,"cities":[{"cityName":"\xe6\xb7\xb1\xe5\x9c\xb3","cityId":1337},{"city
Name":"\xe7\x99\xbd\xe5\xb1\xb1","cityId":10204},{"cityName":"\xe5\x8c\x97\xe4\xba\xac","cityId":2179},{"cityName":"\xe4\xb8\x8a\xe6\xb5\xb7","cityId":121}],"isLogin":true,"isAs":
false,"isPlatformAs":false,"dep":{"depName":"\xe6\xb5\x8b\xe8\xaf\x95\xe8\x87\xaa\xe8\x90\xa5\xe5\x88\x86\xe5\x85\xac\xe5\x8f\xb8","level":1,"parentDepId":null,"orgId":null,"depId
":37,"positionType":2},"staffManagerLevel":0,"orgs":null}}'
  1. json 属性
json: {'code': 200, 'data': {'userId': 6859485, 'userName': '测试陈柏霖', 'realName': '测试陈柏霖', 'mobile': '18688886666', 'email': 'chenbolin@163.com', 'profileUrl': None, 'rol
eId': 1, 'roleName': '新房商服', 'orgId': 3, 'orgName': '测试自营分公司', 'serviceAgentCount': 0, 'cities': [{'cityName': '深圳', 'cityId': 1337}, {'cityName': '白山', 'cityId': 1
0204}, {'cityName': '北京', 'cityId': 2179}, {'cityName': '上海', 'cityId': 121}], 'isLogin': True, 'isAs': False, 'isPlatformAs': False, 'dep': {'depName': '测试自营分公司', 'lev
el': 1, 'parentDepId': None, 'orgId': None, 'depId': 37, 'positionType': 2}, 'staffManagerLevel': 0, 'orgs': None}}
查看上述三种属性类型
print("type:", type(res.json()))
print("type:", type(res.content))
print("type:", type(res.text))

# 输出结果
type: <class 'dict'>
type: <class 'bytes'>
type: <class 'str'>
查看其他属性

打印响应对象其他属性

print("status_code:", res.status_code)   #  可用于断言响应状态码
print("elapsed:", res.elapsed)           #  可用于断言响应时间
print("cookies:", res.cookies)           #  可用于后面接口测试 带上cookie校验
print("url:", res.url)
print("request:", res.request)
print("apparent_encoding:", res.apparent_encoding)
print("close:", res.close)
print("connection:", res.connection)
print("encoding:", res.encoding)
print("history:", res.history)
print("is_permanent_redirect:", res.is_permanent_redirect)
print("is_redirect:", res.is_redirect)
print("iter_content:", res.iter_content())
print("iter_lines:", res.iter_lines())
print("links:", res.links)
print("next:", res.next)
print("ok:", res.ok)
print("raise_for_status:", res.raise_for_status)
print("raw:", res.raw)
print("reason:", res.reason)

输出结果如下:

status_code: 200
elapsed: 0:00:00.242774
cookies: <RequestsCookieJar[<Cookie connect.sid.agentservicewebsiteesffdd=s%3ADOzeKMJ50yAqjQqi_ZSuvyhhHQf3SE-4.YDPfGtNlpfFfUKbSyt5PzCmOn2%2BjoYgSkutEZoXGpfw for ddsf.fangdd.com/>]>
url: https://ddsf.fangdd.com/ioux/loginByPassword
request: <PreparedRequest [POST]>
apparent_encoding: utf-8
close: <bound method Response.close of <Response [200]>>
connection: <requests.adapters.HTTPAdapter object at 0x10fb70a20>
encoding: utf-8
history: []
is_permanent_redirect: False
is_redirect: False
iter_content: <generator object iter_slices at 0x10fb8abf8>
iter_lines: <generator object Response.iter_lines at 0x10fb8abf8>
links: {}
next: None
ok: True
raise_for_status: <bound method Response.raise_for_status of <Response [200]>>
raw: <urllib3.response.HTTPResponse object at 0x10fb9df28>
reason: OK
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值