views:

783

answers:

8

I want to send some strings in a list in a POST call. eg:

    www.example.com/?post_data = A list of strings

The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?

+5  A: 

There's no such thing as a "list of strings" in a URL (or in practically anything in HTTP - if you specify multiple values for the same header, they come out as a single delimited value in most web app frameworks IME). It's just a single string. I suggest you delimit the strings in some way (e.g. comma-separated) and then parse them out again at the other end.

Jon Skeet
+2  A: 

If the big string you're receiving is merely delimited then you could try splitting it. See Splitting strings.

To clarify, you get the delimited list of the strings, split that list into a python list, and voila!, you have a python list...

mepcotterell
what if the string contains a comma? using JSON or serialization would be best imho
kender
+1  A: 

Are you talking about this?

post_data= ",".join( list_of_strings )
S.Lott
+4  A: 

TRY JSON(JavaScript Object Notation) it's available in the python package. Find out here: http://docs.python.org/library/json.html

You can Encode your list to an array represented in JSON and append to the post argument. Later decode it back to list...

yrcjaya
+2  A: 

It depends on your server to format the incoming arguments. for example, when zope gets a request like this: http://www.zope.org?ids:list=1&ids:list=2

you can get the the ids as a list. But this feature depends on the server. If your server does not support some kind of parsing and validating your input, you have to implement it by yourself. Or you use zope.

do3cc
Eli Courtwright
+1  A: 

Data passed to a POST statement is (as far as I understood) encoded as key-value pairs, using the application/x-www-form-urlencoded encoding.

So, I'll assume that you represent your list of string as the following dictionnary :

>>> my_string_list= { 's1': 'I',                                                
...     's2': 'love',                                                           
...     's3': 'python'                                                          
... }

Then, passing it as argument to POST is as difficult as reading the documentation of urllib.

>>> import urllib
>>> print urllib.urlopen( 'http://www.google.fr/search', 
       urllib.urlencode( my_string_list ) 
    ).read()

Note that google does not use POST for its search queries, but you will see the error reported by google.

If you run WireShark while typing the code above, you will see the data of the POST being passed as :

 s3=python&s2=love&s1=I
Bluebird75
A: 

A data structure like django.utils.datastructures.MultiValueDict is a clean way to represent such data. AFAIK it preserves order.

>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
>>> d['name']
'Simon'
>>> d.getlist('name')
['Adrian', 'Simon']
>>> d.get('lastname', 'nonexistent')
'nonexistent'
>>> d.setlist('lastname', ['Holovaty', 'Willison'])

Django is using django.http.QueryDict (subclass of MultiValueDict) to turn a query string into python primitives and back.

from django.http import QueryDict

qs = 'post_data=a&post_data=b&post_data=c'

query_dict = QueryDict(qs)

assert query_dict['post_data'] == 'c'
assert query_dict.getlist('post_data') == ['a', 'b', 'c']
assert query_dict.urlencode() == qs

You should be able to copy these classes and use them in your project. (I haven't checked all dependencies though)

muhuk
+1  A: 

If you can't or don't want to simply separate them with a comma and you want to send them in a more list-ish way. I have a list of numbers that I want to pass and I use a PHP webservice on the other end, I don't want to rebuild my webservice since I'v used a common multiselect element that Zend Framework provided.

This example works fine for me and my little integers and it would with your strings, I actualy don't perform the urllib.quote(s), I just do a str(s).

Import urllib

import urllib

Your list of stings:

string_list = ['A', 'list', 'of', 'strings', 'and', 'öthér', '.&st,u?ff,']

Join together the list of strings with 'post_data[]=', also urlencode the string

post_data = '&'.join('post_data[]='+urllib.quote(s) for s in string_list)

Posts to http://example.com/

urllib.urlopen('http://example.com/',post_data)
Joelbitar
read how to format your code
SilentGhost
Sorry, first time here, i'l get bettet. Promise!
Joelbitar