http请求中的参数
http请求中的参数
本文以前端框架jquery.ajax和后端标准JAX-RS、SpringMVC为例,介绍如何在前后端通过http request消息传递参数。
http请求消息分为消息头header和消息体body。放在body中的数据只有一种,其余都是放在header中的不同位置。
HTTP请求可以文本显示,各参数以回车分割。
如:
一般根据参数在http中的位置的不同,置值和取值的方法也不一样。下面将分别介绍。
header
数据在header中,并单独占header的一个属性。header中除了HTTP标准字段,也可以自己添加非标准字段。
前端:
$.ajax({
url: "/getHeader",
type: "GET",
header: {
token: "11111"
}
});
后端:
JAX-RS
@GET
@Path("/getHeader")
public String getHeader(@HeaderParam(HttpHeaders.token) String token) {}
SpringMVC
@GET
@Path("/getHeader")
public String getHeader(@RequestHeader("token") String token) {}
扩展
HTTP头(Request)中的标准字段如下(摘自维基百科)
字段名 | 说明 | 示例 | 状态 |
---|---|---|---|
Accept-Charset | 能够接受的字符集 | Accept-Charset: utf-8 | 常设 |
Accept-Encoding | 能够接受的编码方式列表。参考HTTP压缩。 | Accept-Encoding: gzip, deflate | 常设 |
Accept-Language | 能够接受的回应内容的自然语言列表。参考 内容协商 。 | Accept-Language: en-US | 常设 |
Accept-Datetime | 能够接受的按照时间来表示的版本 | Accept-Datetime: Thu, 31 May 2007 20:35:00 GMT | 临时 |
Authorization | 用于超文本传输协议的认证的认证信息 | Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== | 常设 |
Cache-Control | 用来指定在这次的请求/响应链中的所有缓存机制 都必须 遵守的指令 | Cache-Control: no-cache | 常设 |
Connection | 该浏览器想要优先使用的连接类型[8] | Connection: keep-alive``Connection: Upgrade | 常设 |
Cookie | 之前由服务器通过 Set- Cookie (下文详述)发送的一个 超文本传输协议Cookie | Cookie: $Version=1; Skin=new; | 常设: 标准 |
Content-Length | 以 八位字节数组 (8位的字节)表示的请求体的长度 | Content-Length: 348 | 常设 |
Content-MD5 | 请求体的内容的二进制 MD5 散列值,以 Base64 编码的结果 | Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== | 过时的[9] |
Content-Type | 请求体的 多媒体类型 (用于POST和PUT请求中) | Content-Type: application/x-www-form-urlencoded | 常设 |
Date | 发送该消息的日期和时间(按照 RFC 7231 中定义的"超文本传输协议日期"格式来发送) | Date: Tue, 15 Nov 1994 08:12:31 GMT | 常设 |
Expect | 表明客户端要求服务器做出特定的行为 | Expect: 100-continue | 常设 |
From | 发起此请求的用户的邮件地址 | From: user@example.com | 常设 |
Host | 服务器的域名(用于虚拟主机 ),以及服务器所监听的传输控制协议端口号。如果所请求的端口是对应的服务的标准端口,则端口号可被省略。[10] 自超文件传输协议版本1.1(HTTP/1.1)开始便是必需字段。 | Host: en.wikipedia.org:80``Host: en.wikipedia.org | 常设 |
If-Match | 仅当客户端提供的实体与服务器上对应的实体相匹配时,才进行对应的操作。主要作用时,用作像 PUT 这样的方法中,仅当从用户上次更新某个资源以来,该资源未被修改的情况下,才更新该资源。 | If-Match: "737060cd8c284d8af7ad3082f209582d" | 常设 |
If-Modified-Since | 允许在对应的内容未被修改的情况下返回304未修改( 304 Not Modified ) | If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT | 常设 |
If-None-Match | 允许在对应的内容未被修改的情况下返回304未修改( 304 Not Modified ),参考 超文本传输协议 的实体标记 | If-None-Match: "737060cd8c284d8af7ad3082f209582d" | 常设 |
If-Range | 如果该实体未被修改过,则向我发送我所缺少的那一个或多个部分;否则,发送整个新的实体 | If-Range: "737060cd8c284d8af7ad3082f209582d" | 常设 |
If-Unmodified-Since | 仅当该实体自某个特定时间已来未被修改的情况下,才发送回应。 | If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT | 常设 |
Max-Forwards | 限制该消息可被代理及网关转发的次数。 | Max-Forwards: 10 | 常设 |
Origin | 发起一个针对 跨来源资源共享 的请求(要求服务器在回应中加入一个‘访问控制-允许来源’('Access-Control-Allow-Origin')字段)。 | Origin: http://www.example-social-network.com | 常设: 标准 |
Pragma | 与具体的实现相关,这些字段可能在请求/回应链中的任何时候产生多种效果。 | Pragma: no-cache | 常设但不常用 |
Proxy-Authorization | 用来向代理进行认证的认证信息。 | Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== | 常设 |
Range | 仅请求某个实体的一部分。字节偏移以0开始。参见字节服务。 | Range: bytes=500-999 | 常设 |
Referer sic(https://zh.wikipedia.org/wiki/HTTP%E5%A4%B4%E5%AD%97%E6%AE%B5#cite_note-11) | 表示浏览器所访问的前一个页面,正是那个页面上的某个链接将浏览器带到了当前所请求的这个页面。 | Referer: http://en.wikipedia.org/wiki/Main_Page | 常设 |
TE | 浏览器预期接受的传输编码方式:可使用回应协议头 Transfer-Encoding 字段中的值;另外还可用"trailers"(与"分块 "传输方式相关)这个值来表明浏览器希望在最后一个尺寸为0的块之后还接收到一些额外的字段。 | TE: trailers, deflate | 常设 |
User-Agent | 浏览器的浏览器身份标识字符串 | User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0 | 常设 |
Upgrade | 要求服务器升级到另一个协议。 | Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 | 常设 |
Via | 向服务器告知,这个请求是由哪些代理发出的。 | Via: 1.0 fred, 1.1 example.com (Apache/1.1) | 常设 |
Warning | 一个一般性的警告,告知,在实体内容体中可能存在错误。 | Warning: 199 Miscellaneous warning | 常设 |
cookie
数据在header的cookie中,cookie是发送HTTP请求时客户端会自动附上的数据。
前端:
$.ajax({
url: "/getCookie",
type: "GET"
})
cookie中的数据
sessionId=415A4AC178C59DACE0B2C9CA727CDD84
后端:
JAX-RS
@GET
@Path("/getCookie")
public String getCookie(@CookieParam("sessionId") String sessionId) {}
SpringMVC
@GET
@Path("/getCookie")
public String getCookie(@CookieValue("sessionId") String sessionId) {}
path
数据在url中。
前端:
$.ajax({
url: "/getUserById/100/Ann",
type: "GET"
})
后端:
JAX-RS
@GET
@Path("/getUserById/{id}/{name}")
public String getUserInfoId(@PathParam("id") int id,
@PathParam("name") String name) {}
SpringMVC
@GET
@Path("/getUserById/{id}/{name}")
public String getUserInfoId(@PathVariable("id") int id,
@PathVariable("name") String name) {}
query
数据在url中,以?
和&
区隔。
前端:
$.ajax({
url: "/getUserByName?name=Ann",
type: "GET"
})
后端:
JAX-RS
@GET
@Path("/getUserByName")
public String getUserInfo(@DefaultValue("fei")@QueryParam("name") String name) {}
SpringMVC
@GET
@Path("/getUserByName")
public String getUserInfo(@RequestParam(value="name", required=false) String name) {}
matrix
数据在url中,以;
区隔。
矩阵参数适用于特定路径元素,而查询参数作为整体应用于请求.当对多级资源和子资源进行复杂的REST风格查询时,这种情况就会发生:
http://example.com/res/categories;name=foo/objects;name=green/?page=1
前端:
$.ajax({
url: "/getMatrix;name=Ann;address=Shanghai",
type: "GET"
});
后端:
JAX-RS
@GET
@Path("/getMatrix")
public String getMatrix(@MatrixParam("name") String name,
@MatrixParam("address") String address) {}
SpringMVC
@GET
@Path("/getMatrix")
public String getMatrix(@MatrixVariable("name") String name,
@MatrixVariable("address") String address) {}
body
一个http请求只能有一个消息体(request body),因此该参数在一个请求中只有一个。
body中的数据支持多种格式,常用的有application/x-www-form-urlencoded
、application/json
、application/xml
等。
后端:
JAX-RS
若数据类型是form,用@FormParam
。
SpringMVC
若数据类型是form,用@RequestParam
。
若数据类型不是form,用@RequestBody
。
其他
获取上下文中的HttpServletRequest、HttpServletResponse等对象。
后端:
JAX-RS
用@Context注解。
public String demo(@Context HttpServletRequest httpServletRequest)
The object instances that it can inject are the following:
SecurityContext – Security context instance for the current HTTP request
Request – Used for setting precondition request processing
Application, Configuration, and Providers -> Provide access to the JAX-RS application, configuration, and providers instances
ResourceContext – Resource context class instances
ServletConfig – The ServletConfig instance instance
ServletContext – The ServletContext instance
HttpServletRequest – The HttpServletRequest instance for the current request
HttpServletResponse – The HttpServletResponse instance for the current request
HttpHeaders – Maintains the HTTP header keys and values
UriInfo – Query parameters and path variables from the URI called
- 点赞
- 收藏
- 关注作者
评论(0)