librelist archives

« back to archive

JSON, POST responses, and quotes

JSON, POST responses, and quotes

From:
Date:
2013-05-23 @ 22:08
Hi,

I have a Flask app that accepts POST requests. I've got some sample code 
to test this:

values = {"id" : ["sdss.272.51941.101.26", "sdss.2537.53906.310.26"],
		  "fields" : "z,ra,dec,target_flags,target_masks",
		  "format" : "json"
		 }

data = urllib.urlencode(values)
response = urllib2.urlopen(url, data)

On the Flask side, when I read request.form["id"], I get this string:

['sdss.272.51941.101.26', 'sdss.272.51941.101.26']

Ideally I'd like to read this back in via JSON, but the JSON syntax 
requires double quote strings, so the above value is not valid. Can I get 
Flask to give me a JSON object, or do I have to parse the raw string 
myself?

Cheers,
Demitri

Re: [flask] JSON, POST responses, and quotes

From:
Steven Kryskalla
Date:
2013-05-23 @ 22:27
On Thu, May 23, 2013 at 3:08 PM,  <thatsanicehatyouhave@me.com> wrote:
> Ideally I'd like to read this back in via JSON, but the JSON syntax 
requires double quote strings, so the above value is not valid. Can I get 
Flask to give me a JSON object, or do I have to parse the raw string 
myself?

The code you posted is turning a python dict into form data and
POSTing it to your flask app.

What you want to do it turn the python dict into JSON and POST that
instead, and with the "application/json" Content-Type.

If you do that, then you can access the JSON object via request.json
(flask will automatically handle it for you).

http://flask.pocoo.org/docs/api/#flask.Request.json

-Steve

Re: [flask] JSON, POST responses, and quotes

From:
Date:
2013-05-24 @ 14:25
Steven, Alex,

Thanks very much for the suggestions. Yes, I'm designing an API and can 
require JSON encoding of POST requests, so that's perfect.

Cheers,
Demitri

Re: [flask] JSON, POST responses, and quotes

From:
Alex Ogier
Date:
2013-05-23 @ 22:49
Agreed. If this is an API endpoint it's fine to have requests be encoded as
JSON.

The other solution is to serialize the "id" field like it was a
select-multiple field in an HTML form. You can do that by passing an
additional doseq argument to urlencode.

urllib.urlencode(values, doseq=True)

That will make the "id" field appear multiple times in the query string.
You can then read it in flask as follows:

request.form.getlist("id")

Best,
Alex Ogier
On May 23, 2013 6:29 PM, "Steven Kryskalla" <skryskalla@gmail.com> wrote:

> On Thu, May 23, 2013 at 3:08 PM,  <thatsanicehatyouhave@me.com> wrote:
> > Ideally I'd like to read this back in via JSON, but the JSON syntax
> requires double quote strings, so the above value is not valid. Can I get
> Flask to give me a JSON object, or do I have to parse the raw string myself?
>
> The code you posted is turning a python dict into form data and
> POSTing it to your flask app.
>
> What you want to do it turn the python dict into JSON and POST that
> instead, and with the "application/json" Content-Type.
>
> If you do that, then you can access the JSON object via request.json
> (flask will automatically handle it for you).
>
> http://flask.pocoo.org/docs/api/#flask.Request.json
>
> -Steve
>