Tencent / Metis

Metis is a learnware platform in the field of AIOps.
Other
1.68k stars 495 forks source link

响应中的中文‘操作成功’未正常编码,实际显示为\u64cd\u4f5c\u6210\u529f #108

Open NamelessAshone opened 3 years ago

NamelessAshone commented 3 years ago

您好!

使用metis的docker镜像测试时,发现响应中的中文显示不正常

日志:

curl -v -X POST -H "Content-Type:application/json;charset=UTF-8"  -d @test1.json http://10.1.70.45:49502/PredictValue
* About to connect() to 10.1.70.45 port 49502 (#0)
*   Trying 10.1.70.45...
* Connected to 10.1.70.45 (10.1.70.45) port 49502 (#0)
> POST /PredictValue HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.1.70.45:49502
> Accept: */*
> Content-Type:application/json;charset=UTF-8
> Content-Length: 3805
> Expect: 100-continue
> 
* Done waiting for 100-continue
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type
< Content-Type: application/json
< X-Frame-Options: SAMEORIGIN
< Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS
* no chunk, no close, no size. Assume close to signal end
< 
* Closing connection 0
{"msg": "\u64cd\u4f5c\u6210\u529f", "code": 0, "data": {"p": "0.00016403946", "ret": 0}}

hexdump保存下来的响应json文件, 中文操作成功被编码为了"\u45fc\u4f5c\u6210\u529"字符串

hexdump -C respone.json 
00000000  7b 22 6d 73 67 22 3a 20  22 5c 75 36 34 63 64 5c  |{"msg": "\u64cd\|
00000010  75 34 66 35 63 5c 75 36  32 31 30 5c 75 35 32 39  |u4f5c\u6210\u529|
00000020  66 22 2c 20 22 63 6f 64  65 22 3a 20 30 2c 20 22  |f", "code": 0, "|
00000030  64 61 74 61 22 3a 20 7b  22 70 22 3a 20 22 30 2e  |data": {"p": "0.|
00000040  30 30 30 31 36 34 30 33  39 34 36 22 2c 20 22 72  |00016403946", "r|
00000050  65 74 22 3a 20 30 7d 7d                           |et": 0}}|
NamelessAshone commented 3 years ago

https://github.com/Tencent/Metis/blob/bf50841faf36cf29378c7b774e6abe4327596bbd/app/controller/render.py#L7

def render_json(dictionary=None):
    dictionary = {} if dictionary is None else dictionary
    response = HttpResponse(json.dumps(dictionary), content_type="application/json")
    response['Access-Control-Allow-Origin'] = '*'
    response["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type"
    response["Access-Control-Allow-Methods"] = "GET, POST, PUT, OPTIONS"
    return response

或许是这部分json.dumps需要修改为json.dumps(dictionary, encoding='utf8', ensure_ascii=False)