【发布时间】:2016-05-20 08:38:18
【问题描述】:
我正在学习 RESTFUL API,但我遇到了一个问题,该问题仅发出 GET 请求,但 POST 请求失败。代码在这里:
from flask import Flask, request
app = Flask(__name__)
#Make an app.route() decorator here
@app.route("/puppies", methods = ['GET', 'POST'])
def puppiesFunction():
if request.method == 'GET':
#Call the method to Get all of the puppies
return getAllPuppies()
elif request.method == 'POST':
#Call the method to make a new puppy
return makeANewPuppy()
def getAllPuppies():
return "Getting All the puppies!"
def makeANewPuppy():
return "Creating A New Puppy!"
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
GET 请求工作正常,但 POST 请求中出现错误。错误是:
127.0.0.1 - - [20/May/2016 01:39:34] "POST /puppies/ HTTP/1.1" 404 -
提前致谢
【问题讨论】:
-
您遇到了什么错误?
-
如何进行 POST 请求? 404 未找到错误。尝试 /puppies 而不是 /puppies/ 并尝试拆分方法,也许它会起作用。
-
对我来说代码工作正常。试试 curl -X POST 0.0.0.0:5000/puppies