简体   繁体   English

烧瓶python-POST无法按预期工作

[英]Flask python - POST not working as expected

I am learning RESTFUL APIs and I am stuck at a problem which only issues request for GET but fails for POST request. 我正在学习RESTFUL API,我陷入一个问题,该问题仅发出GET请求,但发出POST请求失败。 The code goes here: 代码在这里:

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)

The GET request works fine but error coming up in POST request. GET请求工作正常,但POST请求中出现错误。 The error is: 错误是:

 127.0.0.1 - - [20/May/2016 01:39:34] "POST /puppies/ HTTP/1.1" 404 -

Thanks in advance 提前致谢

Your POST request has an extra slash at the end of the URL. 您的POST请求的URL末尾会有一个斜杠。 Here are the curl commands: 这是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>

And here are the Flask logs: 这是烧瓶日志:

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 -

And indeed in your question you used /puppies/ . 实际上,在您的问题中,您使用/puppies/

Your code is working fine, not sure what the problem is. 您的代码运行正常,不确定是什么问题。 i copy pasted your code as follows: 我复制粘贴您的代码,如下所示:

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)

在此处输入图片说明

and the post requests is working as expected. 并且发布请求按预期方式工作。 here is what I did using fiddler: 这是我使用提琴手所做的: 在此处输入图片说明

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

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