V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ropon
V2EX  ›  Python

困惑许久疑问,请帮忙分析下呢,是否方向错了

  •  
  •   ropon ·
    ropon · Aug 2, 2020 · 2256 views
    This topic created in 2098 days ago, the information mentioned may be changed or developed.

    #通过以下代码已解决中文乱码
    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}
    #请问问题出在哪儿呢?
    
    5 replies    2020-08-03 11:57:58 +08:00
    ropon
        1
    ropon  
    OP
       Aug 2, 2020
    hareandlion
        2
    hareandlion  
       Aug 2, 2020
    demo 项目里 requirements.txt 加一下?防止是库版本的问题
    ropon
        3
    ropon  
    OP
       Aug 2, 2020
    @hareandlion 好了,麻烦帮忙看下
    hareandlion
        4
    hareandlion  
       Aug 2, 2020
    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}
    ```
    ropon
        5
    ropon  
    OP
       Aug 3, 2020 via iPhone
    @hareandlion 感谢大佬,我试试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   811 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 111ms · UTC 22:30 · PVG 06:30 · LAX 15:30 · JFK 18:30
    ♥ Do have faith in what you're doing.