在 GET 请求中为相同参数名称传递多个值的正确方法 [英] Correct way to pass multiple values for same parameter name in GET request

查看:223
本文介绍了在 GET 请求中为相同参数名称传递多个值的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究在 GET 请求中为同一参数名称传递多个值的正确方法是什么.

我见过这样的网址:

http://server/action?id=a&id=b

我见过这样的网址:

http://server/action?id=a,b

我的理解是第一个是正确的,但我找不到任何参考.我查看了 http 规范,但没有看到任何关于 URL 的查询"部分应该如何构成的信息.

我不想要一个说两者都可以"的答案——如果我正在构建一个网络服务,我想知道这些方法中的哪一个是标准的,以便使用我的网络服务的人知道如何为同名.

那么,有人可以指出我的官方参考来源以确认哪个选项是正确的吗?

解决方案

确实,没有定义的标准.要支持该信息,请查看维基百科的查询字符串 章节.有以下评论:

<块引用>

虽然没有明确的标准,但大多数 Web 框架允许与单个字段关联的多个值.[3][4]

此外,当您查看 RFC 3986 部分3.4 Query,对于多值的参数没有定义.>

大多数应用程序使用您显示的第一个选项:http://server/action?id=a&id=b.要支持该信息,请查看此 Stackoverflow 链接,以及这个关于 ASP 的 MSDN 链接.NET 应用程序,对具有多个值的参数使用相同的标准.

但是,由于您正在开发 API,我建议您做对您来说最简单的事情,因为 API 的调用者在创建查询字符串时不会遇到太多麻烦.

I'm looking into what is the correct way to pass multiple values for the same parameter name in a GET request.

I've seen URLs like this:

http://server/action?id=a&id=b

And I've seen URLs like this:

http://server/action?id=a,b

My understanding is that the first is correct, but I can't find any reference for this. I had a look at the http spec but couldn't see anything about how the 'query' part of a URL should be made up.

I don't want an answer that says "either is fine" - if I'm building a webservice, I want to know which of these methods is standard so that people using my webservice know how to pass multiple parameters for the same name.

So, can someone point me at an official reference source to confirm which option is correct?

解决方案

Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:

While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field.[3][4]

Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.

Most applications use the first option you have shown: http://server/action?id=a&id=b. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.

However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.

这篇关于在 GET 请求中为相同参数名称传递多个值的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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