#通过以下代码已解决中文乱码
api.representations = {'application/json;charset=utf-8': output_json}
#python 、rust 等测试正常
import requests
res = requests.get(url="http://127.0.0.1:5000/api/v1/login")
print(res.text)
#go 测试异常
resp,_ := http.Get("http://127.0.0.1:5000/api/v1/login")
body,_ :=ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
#错误详情
File "site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "site-packages\flask_restful\__init__.py", line 472, in wrapper
return self.make_response(data, code, headers=headers)
File "site-packages\flask_restful\__init__.py", line 509, in make_response
raise InternalServerError()
#注释之前添加代码,go 测试正常
#api.representations = {'application/json;charset=utf-8': output_json}
#请问问题出在哪儿呢?
1
ropon OP |
2
hareandlion 2020-08-02 22:09:31 +08:00
demo 项目里 requirements.txt 加一下?防止是库版本的问题
|
3
ropon OP @hareandlion 好了,麻烦帮忙看下
|
4
hareandlion 2020-08-02 23:17:27 +08:00
Golang 的 http 包发请求时默认不带 header,没有 header 时 flask 默认的 mediatype 是“application/json”, api.representations 里定义的 key 是'application/json;charset=utf-8',再加一个 key 或者把原来的 key 改成"application/json"就好了
``` api.representations = {'application/json;charset=utf-8': output_json, "application/json": output_json} ``` |
5
ropon OP @hareandlion 感谢大佬,我试试
|