我在服务器端有这个代码
from flask import Flask, request, jsonify
app=Flask(__name__)
@app.route("/pingtest")
def pingtest():
return "Pong!"
@app.route("/registrar_alumno", methods=["POST"])
def registrar_alumno():
print(request.json)
return jsonify(request.json)
app.run(debug=True,port=4000)
在客户机上我有这个代码
import requests
r=requests.post("http://127.0.0.1:4000/registrar_alumno",
data={"test":"hello there"})
print(r.text)
我希望在两侧获得{“test”:“hello there”},但我在服务器上有这样一个:
(asistencias) PS C:\Users\Alumno\Desktop\Proyectos\py\gestion_academica\asistencias> python .\server.py [...] (irrelevants messages that server always shows)
None #...(this should be print(request.json) instruction)
127.0.0.1 - - [15/Mar/2020 16:22:45] "←[37mPOST /registrar_alumno HTTP/1.1←[0m" 200 -
这个在客户机上
(asistencias) PS C:\Users\Alumno\Desktop\Proyectos\py\gestion_academica\asistencias> python .\testclient.py
None
我不知道这是怎么回事,我希望有人能帮我找到错误
之所以会发生这种情况,是因为在客户端请求中使用了
data
参数和字典,该字典发送表单编码数据,导致request.json
返回None
使用
json
参数发送JSON:这将把数据序列化为JSON,并将
Content-Type
头更改为application/json
请参阅more complicated POST requests上的请求文档
相关问题 更多 >
编程相关推荐