Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple key/value pairs in HTTP POST where key is the same name

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.

like image 298
randombits Avatar asked Apr 10 '10 20:04

randombits


People also ask

How do I store multiple values in a single cookie?

To store multiple key-value pairs in a cookie, you have to create a custom object, convert it to a JSON string with the help of the “stringify()” method, and store the resultant “document. cookie” object.

Can a cookie have multiple values?

Can a cookie contain multiple values? A cookie can contain only one “string” value in JavaScript, and storing multiple key-value pairs requires multiple cookies. However, using multiple cookies can also cause some problems.

What are presented in key-value pairs?

A key-value pair (KVP) is a set of two linked data items: a key, which is a unique identifier for some item of data, and the value, which is either the data that is identified or a pointer to the location of that data. Key-value pairs are frequently used in lookup tables, hash tables and configuration files.

Which data type has key-value pair?

Key Value Pair is a standard Experience Data Model (XDM) data type that captures the details of a generic key-value pair.


1 Answers

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.

like image 67
zombat Avatar answered Oct 05 '22 23:10

zombat