python requests post请求体中 含有中文

python requests post请求体中 含有中文 使用json.dumps(data)会把汉字转化为unicode

json.dumps(data,ensure_ascii=False) 报错

报错信息如下:

image.png

python 环境image.png

data 数据

{"baseInfo": {"arrive": "2021-12-12 14:00:00", "ctnEnd": "2021-12-13 16:00:00", "ctnStart": "2021-12-11 16:00:00", "departure": "2021-12-13 07:45:00", "destPort": "PACOL", "destPortName": "COLON FREE ZONE ", "ieFalg": "E", "imo": "9604146", "number": "EGLV143152375694", "numberType": "提单号", "portCloseTime": "2021-12-12 01:00:01", "preArrive": "2021-12-12 13:00:01", "preDeparture": "2021-12-13 07:45:01", "startPort": "CNNGB", "startPortName": "NINGBO", "vesselName": "EVER LENIENT", "voyage": "1035E"}, "step": "装载舱单", "htmldetail": {"提空箱": [], "进仓": [], "重箱进港": [{"箱号": "EITU1421230", "封号": "EMCKCW4121", "进港码头": "梅山码头", "进港时间": "2021-12-14 09:39:00", "箱状态": "整箱"}], "预配舱单": [{"船名": "EVERLENIENT", "航次": "1035E", "提单号": "EGLV143152375694", "电子口岸接收时间": "2021-12-09 14:38:40", "海关回执时间": "2021-12-09 14:40:49", "状态": "21301预配舱单数据传输成功。", "查看": "详情"}], "海关查验": [], "海关放行": [{"船名": "EVERLENIENT", "航次": "1035E", "报关单号": "310120210515568811", "提单号": "EGLV143152375694", "放行时间": "2021-12-14 10:25:08", "通关状态": "查看"}], "码头放行": [{"箱号": "EITU1421230", "状态": "码头不能放行", "原因": "该船已过截关期!", "码头反馈时间": "2021-12-14 11:07:21"}], "装载舱单": [{"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27301装载舱单数据传输成功。", "电子口岸收到时间": "2021-12-14 10:55:43", "收到海关回执时间": "2021-12-14 10:58:14"}, {"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27106该提(运)单未放行,海关审核不通过。", "电子口岸收到时间": "2021-12-13 15:52:44", "收到海关回执时间": "2021-12-13 15:54:33"}, {"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27106该提(运)单未放行,海关审核不通过。", "电子口岸收到时间": "2021-12-13 14:54:42", "收到海关回执时间": "2021-12-13 14:57:24"}, {"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27106该提(运)单未放行,海关审核不通过。", "电子口岸收到时间": "2021-12-13 13:14:40", "收到海关回执时间": "2021-12-13 13:15:49"}, {"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27106该提(运)单未放行,海关审核不通过。", "电子口岸收到时间": "2021-12-13 11:10:38", "收到海关回执时间": "2021-12-13 11:11:58"}, {"提单号": "EGLV143152375694", "船名": "EVERLENIENT", "航次": "1035E", "当前状态": "27106该提(运)单未放行,海关审核不通过。", "电子口岸收到时间": "2021-12-13 09:56:37", "收到海关回执时间": "2021-12-13 09:58:18"}], "重箱装船": [], "开船": [], "船司跟踪": []}}
跪求各位大佬帮忙解决

阅读 6.5k
1 个回答

其实转换为unicode也是比较安全的啊

另你最好提供一下 python版本、数据例子和报错信息。

另建议你先学习一下markdown语法,这样提问的时候 ,数据、程序、内容区分更清晰,方便别人提取信息。

另你还是没有提供报错信息,不知道到底有什么错误?

其实提示中说了,你可以用body.encode('utf-8') 来处理一下,即类似json.dumps(data, ensure_ascii=False).encode('utf-8')来填充需要post的数据变量

即:

response = requests.post( url=url, data = json.dumps(data, ensure_ascii=False).encode('utf-8'), headers={"Content-Type":"application/json"})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏