python获取http请求响应头headers中的数据


例如我要测试一个创建网络的接口,需要先拿token值,而获取token的接口请求成功后,将token存在了响应头headers,postman调接口如下,现在想要通过python获取下图中

X-Subject-Token的值,供后续接口使用

 

 

 

方法:仅需要python的 requests库就可以实现

示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 1.py
# @Author: ttwang
# @Date  : 2022/2/14
# @Desc  :python获取http请求响应头headers中的数据

import requests
import json

base_url = "http://xxxxxxxxx"

param = {
        xxxxxxxx
}

headers = {
    "Content-Type": "application/json",
}

res = requests.post(base_url + "/v3/auth/tokens",headers=headers,json=param,verify=False)
Token = res.headers.get("X-Subject-Token")

 

ps: 同理,如果想获取 Date 和Content_Type ,则:

Date = res.headers.get("Date")
Content_Type = res.headers.get("Content-Type")


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM