一、API测试项目简介
- 项目名称:天气API
- 项目来源:https://www.sojson.com/blog/305.html
- 接口URL:http://t.weather.sojson.com/api/weather/city/+city_code
- 请求方式:GET
- 参数说明:city_code表示城市的Id码,由9为纯数字组成,拼接在以上接口URL的末尾,测试时不需要单独作为参数进行请求
各个城市的city_code码获取地址:http://cdn.sojson.com/_city.json
二、项目API调试
测试案例描述:本次以广州作为被测城市(“city_code”: “101280101”),请求对应城市的天气数据,请求正常情况下,预期将会返回:前一天(yesterday)、当天及未来4天(forecast),一共6天的广州天气数据
拼接成完整的接口URL:http://t.weather.sojson.com/api/weather/city/101280101
1.浏览器调试API
对以上拼接完成的接口URL,在浏览器调试发送请求,响应数据如下:
JSON解析工具①:浏览器自带F12–>Preview
JSON解析工具②:在线JSON校验格式化工具
JSON解析工具③:Json在线解析
2.Python IDE调试API
import requests
# 构造接口测试数据
base_url = 'http://t.weather.sojson.com/api/weather/city'
data = {
'city_code':'101280101'} # 广州的城市Id码
# 发送请求
r = requests.get(base_url+'/'+data['city_code']) # 拼接接口URL
print(r.url)
print('------------------')
# 将返回结果转换为json类型
response_data = r.json()
# 从返回结果中获取(日期、信息、状态、城市)
print(response_data['date']) # 请求的当天日期