简体   繁体   English

Python 瓶子没有来自 React JSON

[英]Python Bottle is None from React JSON

I have this in React:我在 React 中有这个:

const payload = {
    x : x,
    y : y
}

fetch("http://localhost:8080/update_game", {
    method: "POST",
    body: JSON.stringify(payload)})

And this in Python:这在 Python 中:

@post('/update_game')
def update_game():
    req = request.json
    print(req);

I keep getting:我不断得到:

None
127.0.0.1 - - [24/Jan/2022 14:06:36] "POST /update_game HTTP/1.1" 200 0

I don't see what I would be doing different than all of the other examples I've been finding.我看不出我会做什么与我一直在寻找的所有其他示例不同。 payload successfully logs in the console. payload成功登录控制台。 I have CORS set up and working correctly in Python.我在 Python 中设置了 CORS 并正常工作。 I also tried adding contentType: application/JSON to the react side.我还尝试将contentType: application/JSON添加到反应端。 I don't see what I could be doing wrong here.我看不出我在这里做错了什么。

edit for clarity: I'm attempting to print out the json response in the terminal on the python side.为清楚起见进行编辑:我正在尝试在 python 端的终端中打印出 json 响应。 I simply want to see that this response is going through.我只是想看看这种反应正在发生。

I discovered this little bit, that does show something, but is not correct:我发现了这一点,它确实显示了一些东西,但不正确:

@post('/update_game')
def update_game():
    req = request.forms
    print(dir(req))
    print(dict(req))
    print(req)

the ouput:输出:

{'{"x":"0","y":"6"}': ''}

And I also have this:我也有这个:

def update_game():
   req = request.headers['Content-Type']
   # print(dir(req))
   # print(dict(req))
   print(req)

output: output:

text/plain;charset=UTF-8

I'm pretty sure that should be application/JSON我很确定应该是application/JSON

print() only goes to the terminal, and therefore conflicts with your access logs. print()仅进入终端,因此与您的访问日志冲突。 It doesn't return an HTTP response.它不返回 HTTP 响应。

If you want to echo back the request JSON to JS, then you want如果您想将请求 JSON 回显给 JS,那么您想要

@post('/update_game')
def update_game():
    return request.json

And then parse the body from a JS future from fetch()然后从fetch()解析来自 JS 未来的主体

Otherwise, you wanta Response object where you can set status codes, such as 201.否则,您需要一个Response object ,您可以在其中设置状态代码,例如 201。

Adding Content-Type: application/json from your JS wouldn't hurt.从你的 JS 添加Content-Type: application/json不会有什么坏处。

The correct header would be Content-Type: application/json not "contentType: application/JSON".正确的 header 将是Content-Type: application/json而不是“contentType: application/JSON”。 Can you please try the correct header?你能试试正确的 header 吗?

Starting from scratch...从头开始...

$ python3 -m venv venv
$ source venv/bin/activate
$ python -m pip install -U bottle

Create app.py创建app.py

from bottle import post, request, run

@post('/echo')
def echo():
    data = request.json
    print(type(data))
    return data  # example to return the input

run(host='localhost', port=8080, debug=True)

Run python app.py运行python app.py

From a new terminal, I pass the Content-Type along with a payload to return从一个新的终端,我将 Content-Type 与一个有效载荷一起传递给返回

$ curl -H 'Content-Type: application/json' -d'{"x": 2, "y":4}' http://localhost:8080/echo
{"x": 2, "y": 4}

In the server logs, I see在服务器日志中,我看到

<class 'dict'>
127.0.0.1 - - [24/Jan/2022 17:13:38] "POST /echo HTTP/1.1" 200 16

See this post for properly sending JSON from Javascript without it being text/plain - Setting Content-Type in fetch request not working请参阅此帖子以从 Javascript 正确发送 JSON 而不是text/plain - 在获取请求中设置内容类型不起作用

I found one solution.我找到了一种解决方案。 Hopefully this one helps whoever is having the same issues.希望这个可以帮助遇到同样问题的人。

import json
import io


@post('/update_game')
def update_game():
    encoding = "UTF-8"
    req = json.load(io.TextIOWrapper(request.body, encoding=encoding))
    print(req)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM