2016-05-20 11 views
1

Ich lerne RESTFUL APIs und ich bin stecken auf ein Problem, das nur Anfrage für GET gibt aber für POST-Anfrage fehlschlägt. Der Code geht hier:Flask Python - POST funktioniert nicht wie erwartet

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) 

Die GET-Anfrage funktioniert gut, aber Fehler in POST-Anfrage. Der Fehler ist:

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

Vielen Dank im Voraus

+0

* Was * Fehler bekommen Sie? –

+0

Bearbeitet. Vielen Dank. – deep

+1

Wie machen Sie eine POST-Anfrage? 404 wird nicht gefunden Fehler. Versuchen Sie/Welpen statt/Welpen/und versuchen Sie, Methoden zu splitten, vielleicht wird es funktionieren. –

Antwort

5

Ihre POST-Anfrage hat einen zusätzlichen Schrägstrich am Ende der URL. Hier sind die curl Befehle:

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

Und hier sind die Kolben-Protokolle:

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 - 

Und in der Tat in Ihrer Frage, die Sie /puppies/ verwendet.

1

Ihr Code funktioniert gut, nicht sicher, was das Problem ist. Ich kopiere den Code eingefügt wie folgt:

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

und die Post-Anfragen wie erwartet funktioniert. Hier ist, was ich mit Fiddler gemacht habe: enter image description here

Verwandte Themen