1. 程式人生 > 其它 >Requests實現簡單的註冊登入

Requests實現簡單的註冊登入

import requests

# 請求頭
headers = {
    "X-huiya-Media-Type": "huiya.v2"
}
# ===================  註冊介面   =======================
url = "https://huiya-app-company.jia400.com/auth/login"

req_data = {
    "username": "blj",
    "password": "hy1234567",
}
resp = requests.post(url, json=req_data, headers=headers)
print("註冊的響應結果: \n", resp.text) # =================== 登陸介面 ======================= # url地址 url = "https://huiya-app-company.jia400.com/auth/login" # 請求型別:post # 請求體 req_data = { "username": "blj", "password": "hy1234567", } resp = requests.post(url, json=req_data,headers=headers) print("登陸的響應結果: \n
", resp.text) # 提取出來,給到下一介面去作為請求 json_res = resp.json() ##三個字典,一層一層去取值 token = json_res["data"]["token_info"]["token"] user_id = json_res["data"]["id"] # =================== 充值介面 加入鑑權 ======================= # 請求頭 headers = { "X-huiya-Media-Type": "huiya.v2", "Authorization": "Bearer {}
".format(token) } # url url = "https://huiya-app-company.jia400.com/auth/login" # 請求資料 req_data = { "user_id": user_id, "amount": 1000 } res = requests.post(url, json=req_data, headers=headers) print("充值的響應結果: \n", res.json())