python get/post接口使用

背景:

使用python调用get post接口,入参、出参都需要转换,在使用时经常会忘记其中的一步,本文用来记录,后面再使用时直接参考使用

 

代码如下

 

post:

1 headers = {'Content-Type': 'application/json','session':XXXXX}#消息头,根据实际需要添加
2 url = 'http://www.xxxx.com' #地址
3 data = {dataA:a,dataB:b} #入参
4 dataJson = json.dumps(data ) #将 dict转成str
5 res = requests.post(url , data=data , headers=headers) #接口调用
6 
7 resTest = json.loads(res .text) #将返回结果str转成dict

get:

1 headers = {'Content-Type': 'application/json','session':XXXXX}#消息头,根据实际需要添加
2 url = 'http://www.xxxx.com' #地址
3 dataJson = json.dumps(data ) #将 dict转成str
4 res = requests.get(url  , headers=headers) #接口调用
5 
6 resTest = json.loads(res .text) #将返回结果str转成dict

 

posted @ 2019-10-26 17:04  菜小鱼~  阅读(4208)  评论(0编辑  收藏  举报