views:

256

answers:

2

I'm working on an API that accepts data from remote clients, some of which where the key in an HTTP POST almost functions as an array. In english what this means is say I have a resource on my server called "class". A class in this sense, is the type a student sits in and a teacher educates in. When the user submits an HTTP POST to create a new class for their application, a lot of the key value pairs look like:

student_name: Bob Smith
student_name: Jane Smith
student_name: Chris Smith

What's the best way to handle this on both the client side (let's say the client is cURL or ActiveResource, whatever..) and what's a decent way of handling this on the server-side if my server is a Ruby on Rails app? Need a way to allow for multiple keys with the same name and without any namespace clashing or loss of data.

My requirement has to be that the POST data is urlencoded key/value pairs.

A: 

Send your data as XML or JSON and parse whatever you need out of it.

Azeem.Butt
I should have stated above that I need to support urlencoded key/value pairs as a typical browser handles it. I'll edit my question to reflect that.
randombits
+6  A: 

There are two ways to handle this, and it's going to depend on your client-side architecture how you go about doing it, as the HTTP standards do not make the situation cut and dry.

Traditionally, HTTP requests would simply use the same key for repeated values, and leave it up to the client architecture to realize what was going on. For instance, you could have a post request with the following values:

student_name=Bob+Smith&student_name=Jane+Smith&student_name=Chris+Smith

When the receiving architecture got that string, it would have to realize that there were multiple keys of student_name and act accordingly. It's usually implemented so that if you have a single key, a scalar value is created, and if you have multiples of the same key, the values are put into an array.

Modern client-side architectures such as PHP and Rails use a different syntax however. Any key you want to be read in as an array gets square brackets appended, like this:

student_name[]=Bob+Smith&student_name[]=Jane+Smith&student_name[]=Chris+Smith

The receiving architecture will create an array structure named "student_name" without the brackets. The square bracket syntax solves the problem of not being able to send an array with only a single value, which could not be handled with the "traditional" method.

Because you're using Rails, the square bracket syntax would be the way to go. If you think you might switch server-side architectures or want to distribute your code, you could look into more agnostic methods, such as JSON-encoding the string being sent, which adds overhead, but might be useful if it's a situation you expect to have to handle.

There's a great post on all this in the context of JQuery Ajax parameters here.

zombat
+1. The rails params parser will overwrite the value unless you have the [] present.
Tilendor