zl程序教程

您现在的位置是:首页 >  其他

当前栏目

torch serve部署 unicode编码问题curl post如何把返回的unicode编码变成中文字符

中文编码字符部署 如何 返回 post CURL
2023-09-11 14:14:27 时间

解决torch serve http接口返回unicode编码的问题

方案一,curl 测试

curl -X POST http://localhost:8080/predictions/epidemic_hsy -d ‘data=u"2021年1月4日,07:30从家中出发,交通工具无,xxxx"’
返回
在这里插入图片描述

方案二,改成代码访问测试 response.encoding = "unicode_escape"转换编码

参考链接:https://blog.csdn.net/qq_35653145/article/details/107054485


# -*- coding: utf-8 -*-

import requests

question = '2021年1月4日,07:30从家中出发,交通工具无,xxx'.encode('utf-8')
response = requests.post('http://localhost:8080/predictions/epidemic_hsy', data=question)
response.encoding = "unicode_escape"
res = response.text
print(res)

这样就返回正常了

参考:

1、curl 提交数据编码问题#
在 windows 下使用 curl 发送一个 POST 请求的时候,遇到一个问题,传送的参数都不是 UTF-8编码的。

1、在 git-bash 下测试,locale都是 UTF-8,没有作用,服务端收到的还是 GBK 编码。
2、在 cmd 下测试,chcp 65001,没有作用,,服务端收到的还是 GBK 编码。
3、写入脚本文件(sh/bat),在上述两种环境下,都还是 GBK 的。
最后只能是使用 echo 输出数据,然后 curl 从 stdin 读取才行。

Copy
echo ‘{“url”:“这里有中文和zimu”,“time”:1526129881}’ |
curl ‘http://192.168.17.11:8010/testsvr/post’
-H ‘User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0’
-H ‘Accept: application/json, text/plain, /
-H ‘Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2’
-H ‘Content-Type: application/json;charset=utf-8’
-H ‘Origin: http://192.168.17.11:8010’
-H ‘Connection: keep-alive’
-H ‘Referer: http://192.168.17.11:8010/’
-H ‘Cookie: id=cdfb03dfd29351d7b3aeb96b24218b86’
-X POST-d @-