Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Accessing incoming POST data in Flask

Narfanator:

Here's the flask code:


from flask import Flask, request
import json
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def refresh():
params = {
'thing1': request.values.get('thing1'),
'thing2': request.values.get('thing2')
}
return json.dumps(params)

Here's the cURL:


$ curl -XGET 'http://ift.tt/2mHpZyu' -d '{"thing2":2}'
> {"thing1": "1", "thing2": null}

$ curl -XGET 'http://ift.tt/2mHpZyu' -d '{"thing2":2}'
> {"thing1": "1", "thing2": null}

The docs seem VERY clear that this should be working:

form

A MultiDict with the parsed form data from POST or PUT requests. Please keep in mind that file uploads will not end up here, but instead in the files attribute.

args

A MultiDict with the parsed contents of the query string. (The part in the URL after the question mark).

values

A CombinedMultiDict with the contents of both form and args.

Any ideas what I'm doing wrong?

Update: Trying suggestions from one of the answers, swapping out the return line:

Using return json.dumps(json.load(request.body.decode("utf-8") )) generates the error AttributeError: 'Request' object has no attribute 'body'

Using return json.dumps(json.load(request.json)) generates the error AttributeError: 'NoneType' object has no attribute 'read'

Using POST with the origin code appears to have no effect:


$ curl -XPOST 'http://ift.tt/2mHpZyu' -d '{"thing2":2}'
{"thing1": "1", "thing2": null}

Setting the content type and using POST with the original code also has no apparent effect:


$ curl -XPOST -H "Content-Type: application/json" 'http://ift.tt/2mHpZyu' -d '{"thing2":2}'
{"thing1": "1", "thing2": null}

although I went and verified that the content-type was then correctly set:


...
print(request.headers)
...

Host: 127.0.0.1:5000
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
Content-Length: 12



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Accessing incoming POST data in Flask

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×