Python+request 登录接口reponse中token传递给其他接口使用,小示例介绍《一》

要求:

  1、调用登录login

  2、调用通过登录接口返回的reponse中的token和uuid,实现test_create_todo接口的测试

实现:

  1、login登录接口的调用,直接填写对应的URL、headers和data即可。再不需要其他参数的加入

  2、因test_create_todo此接口在登录后,因此需要用到token和uuid,由于是2个不同的方法,因此需要将使用的变量设置为全局变量。

实操作如下:

#!/usr/bin/env python
# coding=UTF-8
import requests

def login():
    url = "https://***.***.com/v2/user/login"   # 注:此处为本公司的host因此不方便外漏
    data = {
        "mobile": "12606666333",
        "password": "33a7d3da476a32ac237b3f603a1be62fad00299e0d4b5a8db8d913104edec629"
    }
    headers = {
        "version": "2.3.0",
        "version-id": "235",
        "device-id": "8BAFD18C-61E3-4BAF-8AB1-0BC16E567633",
        "time": "1557728866628",
        "channel-id": "001",
        "os": "ios",
        "Accept-Language": "zh-tw",
        "device-name": "iPhoneX",
        "User-Agent": "iBer/235 CFNetwork/976 Darwin/18.2.0",
        #注:一定不能加content-type,否则报签名错误
        # Content-Type: multipart/form-data; boundary=vHsPJ5s6grMzpmWoZxd3T3o6.LcUWBvUUu0gDNubaf05Ve7kv6bAkH3s_rr0AEc2D6AbEh
        "sign": "a81b4379f504f330e83792ce2015e629"
    }

    r = requests.post(url=url, data=data, headers=headers)
    #global uuid,token,version,version_id
    global uuid
    uuid = str(r.json()["data"]["uuid"])
    global token
    token = str(r.json()["data"]["token"])
    version = "2.2.1"
    version_id = "232"
    print "登录成功,如下是reponse返回的内容"
    print r.text

    #print uuid,token,version,version_id

def test_create_todo():
    url = "https://***.***.com/v2/todo/create"
    data = {
        "name": "1239",
        "todo_remind_type": "0",
        "cate_uuid":"86799e50d9890ade579c4ac88059a5ff",
        "all_day":"1",
        "todo_start":"2019-05-13",
        "todo_end":"",
        "type":"0",
        "repeat_tyep":"0",
        "c_user_uuid":""
    }
    headers = {
        "version": "2.3.0",
        "version-id": "235",
        "os": "ios",
        "sign": "123456",
        "is-test":"1",
        "uuid":uuid,
        "token":token
    }
    print uuid
    print token
    r = requests.post(url=url, data=data, headers=headers)
    print "%%%%%%%%%%%%%%%%%%"
    print r.json()

login()
test_create_todo()

 

注:使用postman工具的执行步骤,可参考此链接:https://www.cnblogs.com/syw20170419/p/10858645.html

原文地址:https://www.cnblogs.com/syw20170419/p/10896573.html