1

In my coffeescript frontend I attempt to pass a list of value to the backend

data = {
  id: [2, 3, 4]
}

$.post url, data

In the handler in a Google app engine (python) backend, I read the value like so:

    id_value = self.request.get('id')

    LOG.info("%s", id_value)

It always print out '2' only.

How can I get the backend to obtain the list [2,3,4]?

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

2 Answers2

0

$.post by default sends data in url-encoded format, which handles nested structures in its own way.

You might need to encode the data in JSON before sending and then decode it on the server side - an example is here.

Community
  • 1
  • 1
Aleph Aleph
  • 5,215
  • 2
  • 13
  • 28
0

The request object provides a get() method that returns values for arguments parsed from the query and from POST data.

If the argument appears more than once in a request, by default get() returns the first occurrence. To get all occurrences of an argument that might appear more than once as a list (possibly empty), give get() the argument allow_multiple=True.

Hence you should use something like the below snippet. You can find more details here.

id_value = self.request.get('id', allow_multiple=True)

If you need to access variables url encoded in the body of a request (generally a POST form submitted using the application/x-www-form-urlencoded media type), you should use something like this.

id_value = self.request.POST.getall('id')
Community
  • 1
  • 1
pgiecek
  • 7,970
  • 4
  • 39
  • 47