Home > Enterprise >  how to use CURL to post a message like field=value , i used -d and -f but it all failed
how to use CURL to post a message like field=value , i used -d and -f but it all failed

Time:10-13

@app.route('/test')
def test():
    msg = request.form['msg']
    return msg
curl -X POST -d 'msg = test' 127.0.0.1:5000/test
curl -X POST -F 'msg = test' 127.0.0.1:5000/test
The method is not allowed for the requested URL
127.0.0.1 - - [13/Oct/2022 03:18:18] "POST /test HTTP/1.1" 405

CodePudding user response:

You are making a POST request with curl, but you have only defined your route for a GET request. You need to write:

@app.route('/test', methods=['POST'])
def test():
    msg = request.form['msg']
    return msg

And then use the -F argument to curl:

curl -F msg=test localhost:5000/test

CodePudding user response:

The error to lookout for is

The method is not allowed for the requested URL

You may require both POST and GET here. Not mentioning methods parameter in @app.route() defaults it to GET only. So, you would need to add that parameter to enable serving POST requests.

So, your first line should be as

@app.route('/test', methods=['POST', 'GET'])

and your problem should be resolved.

  • Related