将表单 POST 与查询字符串结合使用是否有效? [英] Is it valid to combine a form POST with a query string?

查看:58
本文介绍了将表单 POST 与查询字符串结合使用是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,例如,在大多数 MVC 框架中,查询字符串参数和表单参数都将提供给处理代码,并且通常合并为一组参数(通常以 POST 优先).但是,根据 HTTP 规范这样做是否有效?假设您要发布到:

http://1.2.3.4/MyApplication/Books?bookCode=1234

... 并提交一些更新,例如更改书籍代码为 1234 的书籍名称,您会希望处理代码同时考虑 bookCode 查询字符串参数,以及带有更新的书籍信息的 POSTed 表单参数.这是有效的吗?这是个好主意吗?

解决方案

根据 HTTP 规范是否有效?

.

以下是这些规范中定义的 URL 的一般语法

http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?"询问 ]]

http_URL 的形式没有额外的限制.特别是 http 方法(即 POST,GET,PUT,HEAD,...) 使用时不对 http URL 格式添加任何限制.

使用GET方法时:服务器可以认为请求体为空.

使用 POST 方法时:服务器必须处理请求正文.

<块引用>

这是个好主意吗?

这取决于您需要做什么.我建议您使用这个 link 来解释 GET 和 POST 背后的想法.>

我认为在某些情况下,在 url 的 query 部分始终包含一些参数(例如用户语言)会很方便.

I know that in most MVC frameworks, for example, both query string params and form params will be made available to the processing code, and usually merged into one set of params (often with POST taking precedence). However, is it a valid thing to do according to the HTTP specification? Say you were to POST to:

http://1.2.3.4/MyApplication/Books?bookCode=1234

... and submit some update like a change to the book name whose book code is 1234, you'd be wanting the processing code to take both the bookCode query string param into account, and the POSTed form params with the updated book information. Is this valid, and is it a good idea?

解决方案

Is it valid according HTTP specifications ?

Yes.

Here is the general syntax of URL as defined in those specs

http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

There is no additional constraints on the form of the http_URL. In particular, the http method (i.e. POST,GET,PUT,HEAD,...) used don't add any restriction on the http URL format.

When using the GET method : the server can consider that the request body is empty.

When using the POST method : the server must handle the request body.

Is it a good idea ?

It depends what you need to do. I suggest you this link explaining the ideas behind GET and POST.

I can think that in some situation it can be handy to always have some parameters like the user language in the query part of the url.

这篇关于将表单 POST 与查询字符串结合使用是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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