1

I am learning RESTFUL APIs and I am stuck at a problem which only issues request for GET but fails for POST request. 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. The error is:

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

Thanks in advance

3
  • What error do you get? Commented May 20, 2016 at 8:39
  • 1
    How do you do POST request? 404 is not found error. Try /puppies instead of /puppies/ and try to split methods, maybe it will work. Commented May 20, 2016 at 8:43
  • For me the code works fine. Try curl -X POST 0.0.0.0:5000/puppies Commented May 20, 2016 at 8:46

2 Answers 2

6

Your POST request has an extra slash at the end of the URL. Here are the curl commands:

$ 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/.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

enter image description here

and the post requests is working as expected. here is what I did using fiddler: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.