首页 > 其他分享 > requests 响应头部转json时报错TypeError: Object of type CaseInsensitiveDict is not JSON serializable

requests 响应头部转json时报错TypeError: Object of type CaseInsensitiveDict is not JSON serializable

时间:2023-09-25 19:44:32浏览次数:48  
标签:TypeError get Object json headers JSON print requests type

前言

requests 响应头部在转json时,想格式化输出,结果报错TypeError: Object of type CaseInsensitiveDict is not JSON serializable

报错详情

示例代码

import requests
import json
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/


r = requests.get('http://httpbin.org/get')
print(r.headers)
print(json.dumps(r.headers, indent=4))

运行后报错

{'Date': 'Mon, 25 Sep 2023 11:32:31 GMT', 'Content-Type': 'application/json', 'Content-Length': '307', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
Traceback (most recent call last):
  File "D:/demo/untitled1/a0_xue/__init__.py", line 7, in <module>
    print(json.dumps(r.headers, indent=4))
  File "D:\python3.8\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "D:\python3.8\lib\json\encoder.py", line 201, in encode
    chunks = list(chunks)
  File "D:\python3.8\lib\json\encoder.py", line 438, in _iterencode
    o = _default(o)
  File "D:\python3.8\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type CaseInsensitiveDict is not JSON serializable

虽然r.headers 打印出来看到的是字典类型,其实并不是真正的字典, 它是CaseInsensitiveDict 类型

import requests
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/

r = requests.get('http://httpbin.org/get')
print(type(r.headers))  # <class 'requests.structures.CaseInsensitiveDict'>

所以是没法通过json.dupms() 转成json数据。

解决方法

知道报错原因解决就很简单了,只需转成 dict 类型即可

import requests
import json
# 上海悠悠 wx:283340479
# blog:https://www.cnblogs.com/yoyoketang/

r = requests.get('http://httpbin.org/get')

# 响应头部
response_header = json.dumps(dict(r.headers), indent=4)
print(f'响应头部: {response_header}')

# 请求头部
request_header = json.dumps(dict(r.request.headers), indent=4)
print(f'请求头部: {request_header}')

运行结果

响应头部: {
    "Date": "Mon, 25 Sep 2023 11:38:01 GMT",
    "Content-Type": "application/json",
    "Content-Length": "307",
    "Connection": "keep-alive",
    "Server": "gunicorn/19.9.0",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": "true"
}
请求头部: {
    "User-Agent": "python-requests/2.28.2",
    "Accept-Encoding": "gzip, deflate",
    "Accept": "*/*",
    "Connection": "keep-alive"
}

标签:TypeError,get,Object,json,headers,JSON,print,requests,type
From: https://www.cnblogs.com/yoyoketang/p/17728399.html

相关文章

  • Selenium模拟登录(数字验证码)+Requests获取json数据
    前言我只是想算算每个月洗澡花了多少钱……Selenium模拟登录登录需要提交账号、密码、验证码#实例化browser=webdriver.Edge()browser.implicitly_wait(10)url1='http://card.cqu.edu.cn/'browser.get(url1)#学工号和密码user_id='学号/工号'user_psd='密码'#输入学......
  • 使用json+echarts实现数据库数据的可视化
    因为我上次是使用的另外一种方法实现的可视化,对json了解的不够,所以来补充一下这一块的知识点1、新建一个SpringBoot项目2、添加依赖<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId></dependency><d......
  • Revit二次开发之 GeometryObject分析
    Revit包含了一套完整的几何库,这些几何对象都继承自GeometryObject对象,根据分类,我们可以知道,图形元素的集成关系如下: 1、Autodesk.Revit.DB.Arc对象,当前主要用于一些标尺对象GeometryElement这个是比较核心的元素,Element通过get_Geometry函数,能够获取的元素就是这个元素,这......
  • Uncaught TypeError: Cannot read properties of undefined (reading 'form')问题的解
    问题描述使用vue3执行数据添加操作时,发现了这个错误,使用测试按钮拿文本框数据,一直报错拿不到:问题解决原来是vue2在执行这个操作时:里面放this.form;而vue3在执行这个操作时,里面放的却是:form......
  • MySQL——处理JSON类型的数据
    MySQL对JSON类型数据的处理参考视频:快速学习MySQL8JSON注意,本文的键名也可以叫key,键值也可以叫value,意思是一样的1.字符串查询:JSON_EXTRACT假设我们有一个表叫做testDemo,其中有一个字段叫做details,类型为JSON,他的数据结构如下,我们就以这个结构为例(这是个例子,假设有很多行......
  • 14.JSON之间的相互转换
    Javascript中任何支持的类型都可以转换为JSON字符串对象{}数组【】所有的键值对key;valuevarasd={name:'猴王',age:123,nl:12234123}//对象转化为JSON字符串varaaa=JSON.stringify(asd);//JSON字符串转化为对象varabc=JSON.parse('{......
  • C#中使用Newtonsoft.Charp实现Json对象序列化与反序列化
    场景C#中使用Newtonsoft.Json实现对Json字符串的解析:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/105795048上面讲的对JSON字符串进行解析,实际就是JSON对象的反序列化。在与第三方进行交互时常需要封装对象,存储各种属性消息,然后将对象序列化为json字符串并进......
  • [WPF] 随笔1:MVVM在ViewModel更新Image控件的BitmapImage值时报:必须在与 DependencyObj
    MVVM在ViewModel更新Image控件的BitmapImage值时报:必须在与DependencyObject相同的线程上创建DependencySource原因:必须在UI线程创建BitmapImage=>链接解决方案:使用MemoryStream加载图片,并在UI线程转换成BitmapImage=>链接接下来是我的写法Tip:我用的是MVVMLightViewM......
  • python的配置文件 ini 类型/json类型
    ini类型会把所有的value都改成str类型,而json会保持原本value的类型不变。1.ini/cnf等类型配置文件cfg.cfg[mysql]host="10.12.7.154"port=31066user="root"password="xxx"charset="utf8"database="project_database"使用配置文件from......
  • PostgreSQL教程:JSON&JSONB类型
    JSON在MySQL8.x中也做了支持,但是MySQL支持的不好,因为JSON类型做查询时,基本无法给JSON字段做索引。PGSQL支持JSON类型以及JSONB类型。JSON和JSONB的使用基本没区别。撇去JSON类型,本质上JSON格式就是一个字符串,比如MySQL5.7不支持JSON的情况的下,使用text也可以,但是字符串类型无法校验......