烧瓶python-POST未按预期工作

烧瓶python-POST未按预期工作,python,rest,flask,Python,Rest,Flask,我正在学习RESTfulAPI,我遇到了一个问题,这个问题只对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 == 'GE

我正在学习RESTfulAPI,我遇到了一个问题,这个问题只对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 -

提前感谢

您的代码工作正常,不确定问题出在哪里。我复制粘贴了您的代码,如下所示:

from flask import Flask, render_template, request

app = Flask(__name__)

@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.run(debug=True)

并且post请求正在按预期工作。下面是我使用fiddler所做的:

您的代码工作正常,不确定问题出在哪里。我复制粘贴了您的代码,如下所示:

from flask import Flask, render_template, request

app = Flask(__name__)

@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.run(debug=True)

并且post请求正在按预期工作。下面是我使用fiddler所做的:
您的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>

事实上,在你的问题中,你使用了
/puppies/

你的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>

事实上,在你的问题中,你使用了
/puppies/

你得到了什么错误?你如何处理POST请求?404找不到错误。尝试/puppies而不是/puppies/并尝试拆分方法,也许它会起作用。对我来说,代码工作得很好。尝试curl-xpost您会得到什么错误?如何处理POST请求?404找不到错误。尝试/puppies而不是/puppies/并尝试拆分方法,也许它会起作用。对我来说,代码工作得很好。试试curl-X POST