eddie
eddie

Reputation: 415

Flask not receiving arguments from curl post

Trying to implement a flask API with the following code:

import tweepy,flask,os,flask_api,re,json,logging    
app = flask.Flask(__name__,template_folder='')
@app.route('/',methods=['POST'])
def answer():
    app.logger.info('server responding')
    for key,value in flask.request.args.items():
        app.logger.info(key+':'+value)
    return 'Server on line'

@app.route('/',methods=['GET'])
def home():
    return flask.render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)#use_reloader=False
    logging.basicConfig(filename='error.log',level=logging.DEBUG)
    app.logger.info('Successfull info message')
    app.logger.error('Successfull error message')

When I try to curl it wit windows,

curl -X POST -d query=cheese http://127.0.0.1:5000/

(long form)

curl --header "Content-Type: application/json"  --request POST  --data '{"query":"cheese"}'   http://127.0.0.1:5000/

I get

 INFO in logtest: server responding

But not a single key/value pair is returned

Upvotes: 1

Views: 1820

Answers (1)

rdas
rdas

Reputation: 21285

flask.request.args are URL query parameters.

curl --data is body.

You're sending a request body but expecting them to show up in the URL params. Which will obviously not work.

You need to access request.data instead. There is a convenience method get_json() that you can use to have Flask parse the body into a dictionary for you.

Upvotes: 4

Related Questions