WebOb POST vars like in PHP?

57 views
Skip to first unread message

Vlad K.

unread,
Jun 25, 2011, 8:34:57 AM6/25/11
to pylons-...@googlegroups.com

I guess this has probably been asked before, but Google did not return
anything meaningful.

My question is what would be the best way to supply POST vars in PHP
notation, that would be converted to a dictionary (in Pyramid):

var[foo]=123 -> becomes request.POST["var"]["foo"] with value 123
var[bar]=456 -> becomes request.POST["var"]["bar"] with value 456

I can write my own function that extracts these from request.POST, but I
was wondering if I've missed some documentation or an already existing
lib? Now, I know I can handle hierarchical post data with, say, a JSON
content type post, but I'm looking for a solution to support client
developed for PHP backend, without having to rewrite it.

Thanks!

--

.oO V Oo.

Thomas G. Willis

unread,
Jun 25, 2011, 9:05:39 AM6/25/11
to pylons-...@googlegroups.com

Toscawidgets is another way but that library may not be as stable as you would like at the moment. 




Stonly Baptiste

unread,
Jun 25, 2011, 9:28:06 AM6/25/11
to pylons-...@googlegroups.com
>>> import simplejson >>> mypost = request.params.POST('var')
>>> json.loads('[%s]' % mypost[:-1])

Or something like that

You wouldn't have to change your php to send json, python could convert it to json

 
Thank you,
Stonly Baptiste
Aston UC is now Veddio Cloud Solutions
954.510.9642-Phone ext.104
877.357.7727-Toll Free ext.104
877.403.7036-Fax
sto...@veddio.com- Email
http://www.veddio.com- Web


Sent via BlackBerry from T-Mobile
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Vlad K.

unread,
Jun 26, 2011, 6:48:45 AM6/26/11
to pylons-...@googlegroups.com


Thanks for the suggestions.


.oO V Oo.
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/5pKb0dr1Pp0J.

Vlad K.

unread,
Jun 26, 2011, 7:04:47 AM6/26/11
to pylons-...@googlegroups.com

Thanks for your reply, but the problem is there is no
request.POST("var"), there is request.POST("var[foo]") and at that point
I don't know all the possible values of "foo". I solved with with a
custom extraction function.


While I'm at it, though, I wonder how others are solving the same
problem. In PHP there are no sequences and dicts, just one associative
array entity, so things like:

var['a']['b'] = c

are perfectly valid ad hoc, even if var was never used before, while
Python would complain about missing key 'a' in dict var.

So the actual problem is this. I have a form with a table. Columns are
entity fields, rows are individual entities, just like in a database.
For a PHP backend the form would be constructed as:

<input type="text" name="foo[1]" value="123" />
<input type="text" name="bar[1]" value="123" />
<input type="text" name="baz[1]" value="123" />
<input type="text" name="gah[1]" value="123" />

with that being single row, entities having columns foo, bar, baz and
gah, and the value in brackets is the row ID. One solution would be:

<input type="text" name="foo" value="123" />
<input type="text" name="bar" value="123" />
<input type="text" name="baz" value="123" />
<input type="text" name="gah" value="123" />
<input type="hidden" name="id" value="1" />

But I can't trust that the order given in the form is the same given
through POST and especially the same reconstructed via WebOb's multidict
entities (since Python's dicts are not ordered).

Another solution would be to serialize the form using, say, jQuery and
do a JSON content type POST, then simply do json.loads(request.body) on
the server side, but as I said, I don't want to modify the client if I
don't have to. The best I can do is regex replace foo[id] into foo-id
for easier parsing (field.split("-")).


Suggestions (I mean, other than using a lib or function that parses
foo[id], bar[id], ...)?


.oO V Oo.

Blaise Laflamme

unread,
Jun 26, 2011, 9:33:48 AM6/26/11
to pylons-discuss
Formencode uses keys with . for nested dictionaries and -int for
ordered lists, take a look:

http://formencode.org/Validator.html#http-html-form-input

Chris Withers

unread,
Jun 26, 2011, 10:45:14 AM6/26/11
to pylons-...@googlegroups.com, Vlad K.
On 26/06/2011 12:04, Vlad K. wrote:
>
> Thanks for your reply, but the problem is there is no
> request.POST("var"), there is request.POST("var[foo]") and at that point
> I don't know all the possible values of "foo". I solved with with a
> custom extraction function.

I'd strongly recommend you look at colander:

http://docs.pylonsproject.org/projects/colander/dev/

...if you just want extraction of structured data from requests.

If you want full form generation, then move on to using deform.

cheers,

Chris

> But I can't trust that the order given in the form is the same given
> through POST

Yes, you can.

> and especially the same reconstructed via WebOb's multidict
> entities (since Python's dicts are not ordered).

WebOb's multidicts are ordered.

> Suggestions (I mean, other than using a lib or function that parses
> foo[id], bar[id], ...)?

If you have to re-write a server without changing a client form then
yes, your best bet *is* to write a function that mimics what PHP's
extraction does.

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Chris McDonough

unread,
Jun 26, 2011, 2:18:53 PM6/26/11
to pylons-...@googlegroups.com

This is actually not quite true. See http://plope.com/peppercorn (which
might be something you'd want to use).

- C


Vlad K.

unread,
Jul 1, 2011, 6:24:20 AM7/1/11
to pylons-...@googlegroups.com

On 06/26/2011 08:18 PM, Chris McDonough wrote:
> This is actually not quite true. See http://plope.com/peppercorn (which
> might be something you'd want to use).
>

Yes, I wrongly accused MultiDict of being unordered, so I ran through
the source to see how it works. And indeed, peppercorn is something that
would fit my needs perfectly, so thanks for that!


V

Reply all
Reply to author
Forward
0 new messages