Keren_H
Keren_H

Reputation: 101

Python flask request

I'm in the middle of developing a Python app and using flask. I am now writing a POST function that should add a message to a database. Here is the function I wrote:

@app.route('/AddMessage', methods=['POST'])
def AddMessage():
    m=Message(session_id=1, user_id=user.applicatio_id, content='some message', participants=['Ben','Keren','john'])
    db.session.add(m)
    db.session.commit()
    return 'ok'

But it sends me such an error message:

    Method Not Allowed
The method is not allowed for the requested URL.

What's wrong with my POST function?

Upvotes: 0

Views: 37

Answers (1)

SunPaz
SunPaz

Reputation: 160

As you mentionned in the comments,

You are trying to reach your endpoint calling 127.0.0.1:5000/AddMessage in your browser.

Doing this, you are implicitly calling a GET on 127.0.0.1:5000/AddMessage. My recommendation is trying to call it using curl or Javascript to make a POST call.

In the meantime, for a debug purpose, you could just add "GET" to your accepted methods.

Upvotes: 1

Related Questions