Flask python - POST没有按预期工作

dee*_*eep 1 python rest flask

我正在学习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)
Run Code Online (Sandbox Code Playgroud)

GET请求工作正常但POST请求中出现错误.错误是:

 127.0.0.1 - - [20/May/2016 01:39:34] "POST /puppies/ HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

提前致谢

Ale*_*all 5

您的POST请求在URL的末尾有一个额外的斜杠.这是curl命令:

$ curl -X POST 0.0.0.0:5000/puppies
Creating A New Puppy
$ curl -X POST 0.0.0.0:5000/puppies/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
Run Code Online (Sandbox Code Playgroud)

以下是Flask日志:

127.0.0.1 - - [20/May/2016 11:17:12] "POST /puppies HTTP/1.1" 200 -
127.0.0.1 - - [20/May/2016 11:17:20] "POST /puppies/ HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)

确实在你提出的问题中/puppies/.