HTTP POST 中的多个键/值对,其中键的名称相同 [英] Multiple key/value pairs in HTTP POST where key is the same name

查看:53
本文介绍了HTTP POST 中的多个键/值对,其中键的名称相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 API,它接受来自远程客户端的数据,其中一些在 HTTP POST 中的键几乎可以作为数组使用.用英语来说,这意味着我的服务器上有一个名为class"的资源.从这个意义上说,类是学生所在的类型和教师教育的类型.当用户提交 HTTP POST 为其应用程序创建新类时,许多键值对如下所示:

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

在客户端处理此问题的最佳方法是什么(假设客户端是 cURL 或 ActiveResource,等等),如果我的服务器是 Ruby on Rails,那么在服务器端处理此问题的最佳方法是什么应用程序?需要一种方法来允许多个具有相同名称的键并且不会发生任何命名空间冲突或数据丢失.

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.

我的要求必须是 POST 数据是 urlencoded 键/值对.

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

推荐答案

有两种方法可以解决这个问题,这将取决于您的客户端架构如何去做,因为 HTTP 标准没有规定形势急转直下.

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.

传统上,HTTP 请求只会对重复的值使用相同的键,并让客户端架构来了解发生了什么.例如,您可以有一个具有以下值的 post 请求:

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

当接收架构获得该字符串时,它必须意识到有多个 student_name 键并采取相应的行动.它通常是这样实现的,如果您只有一个键,则会创建一个标量值,如果您有多个相同的键,则将这些值放入一个数组中.

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.

不过,现代客户端架构(例如 PHP 和 Rails)使用不同的语法.您想作为数组读入的任何键都会附加方括号,如下所示:

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

接收架构将创建一个名为student_name"的不带括号的数组结构.方括号语法解决了无法发送只有单个值的数组的问题,这是传统"方法无法处理的.

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.

因为您使用的是 Rails,所以方括号语法将是可行的方法.如果您认为您可能会切换服务器端架构或想要分发您的代码,您可以研究更多不可知的方法,例如对正在发送的字符串进行 JSON 编码,这会增加开销,但如果这是您期望的情况,则可能会很有用必须处理.

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.

JQuery Ajax 的上下文中,有一篇关于所有这些的很棒的帖子参数在这里.

这篇关于HTTP POST 中的多个键/值对,其中键的名称相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆