zoukankan      html  css  js  c++  java
  • 基于python实现GET和POST请求及token相关调用

    GET请求实例: 

    #coding:utf-8
    import  requests
    parm={"userId":"YIN","password":"123"}#封装登录参数
    r=requests.get("http://space.test.com/zhzlApp/user/login",params=parm)#发送get请求
    
    c= r.json()#将请求响应response值json格式化
    respon=(c['data']['accountUsers'][0]['userId'])#获取userId的值
    respon2=(c['data']['accountUsers'][1]['userName'])#获取userName值
    if r.status_code ==200:
        if respon == 'YIN':
            print ('登录成功')#判断登录的用户是否正确
        else :
            print ('登录失败')
    else:
        print('接口报错了')
    POST请求实例:
    #coding:utf-8
    import requests
    
    pay={"userId":"system","password":"123456"}#封装请求参数
    r=requests.post('http://space.test.com/zhzlApp/user/newLogin',data=pay)#发起post登录接口请求
    c=r.json()#接口响应参数json化
    #print(json.dumps(r.json(),encoding='utf-8',ensure_ascii=False,indent=4))#将返回内容通过json格式显示
    
    userId=(c['data']['userId'])#获取userId的值
    print(r.headers)
    token=r.cookies['CIGToken']
    if userId=='SYSTEM':
        print('登录成功')
    else:
        print ('登录失败')
    ----通过登录接口获取token值给下个接口使用---
    head={'Cookie': "CIGToken=" + token,  'Content-Type':
        'application/x-www-form-urlencoded;charset=UTF-8'}#将登录的head信息保存起来,让下个接口调用
    pay2={"keyword":"","personType":""}#封装参数
    fimily=requests.get('http://space.test.com/zhzlbackend/realPerson/person/familyPersons1',params=pay2,headers=head)#发送get请求
    base=fimily.json()#接口响应参数json化
    total=(base['data']['total'])#获取响应中的total数据
    if(total ==651199):#判断数量与实际是否一致
        print('户籍人口列表查询接口正常')#打印日志
    else:
        print('接口数据不对了')
     


  • 相关阅读:
    样式问题
    布局
    通用模板实现可变参数函数
    使用单例模式实习string类
    动态生成一维数组和二维数组
    自定义的类传数据到主窗口控件上的方法
    使用TableView
    G480折腾上了黑苹果,完美了,哈哈
    error C2383: 此符号中不允许有默认参数
    动态链接库的隐式动态链接和显示动态链接
  • 原文地址:https://www.cnblogs.com/yinrw/p/10756405.html
Copyright © 2011-2022 走看看