例如我要测试一个创建网络的接口,需要先拿token值,而获取token的接口请求成功后,将token存在了响应头headers,postman调接口如下,现在想要通过python获取下图中
X-Subject-Token的值,供后续接口使用
方法:仅需要python的requests库就可以实现
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/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 ,则:
1
2
|
Date = res.headers.get("Date") Content_Type = res.headers.get("Content-Type") |
到此这篇关于python获取http请求响应头headers中的数据的文章就介绍到这了,更多相关python http响应头内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/wwwwtt/p/15892233.html