码迷,mamicode.com
首页 > 其他好文 > 详细

关于 GET、POST、表单、Content-Type

时间:2019-04-16 16:30:07      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:pcl   ipa   标签   form 表单   headers   anti   消息   区别   bin   

关于 GET、POST、表单、Content-Type


headers

HTTP 的请求头,在这里也可以放参数,不论 GET 请求或 POST 请求都可以。


GET 请求

GET 请求的参数都在 URL 上,即使用 GET 提交表单请求,也会把表单中的参数以 "?a=xxx&b=xxx" 这种形式发送到后台。

GET 请求没有消息体,所有 GET 请求的 headers 中不会有 content-type 字段,你可以在浏览器中试一下。


没有调查就没有发言权啊,GET 请求也可以发送消息体,啪啪啪打脸了。


用 Ajax 发送 GET 请求,data 中可以传 json,但还是会编码到 URL 上。


For the sake of safety, no password in GET。



POST 请求


POST 请求的数据会放在消息体中,当然 URL 上也可以带参数。


消息体中的数据需要编码,我分为了 3 中方式,每种方式下又分若干的编码格式:


form 表单

POST 上传表单有两种编码格式,编码格式在 headers 中用 Content-Type 字段表示。

  • Content-Type: application/x-www-form-urlencoded
  • Content-Type: multipart/form-data

x-www-form-urlencoded:只能上传键值对,并且键值对都是间隔分开的;application/x-www-from-urlencoded,会将表单内的数据转换为键值对,比如 name=java&age = 23

multipart/form-data:它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。当上传的字段是文件时,会有 Content-Type 来说明文件类型;content-disposition,用来说明字段的一些信息;由于有 boundary 隔离,所以 multipart/form-data 既可以上传文件,也可以上传键值对,它采用了键值对的方式,所以可以上传多个文件。


multipart/form-data 与 x-www-form-urlencoded 区别

multipart/form-data:既可以上传文件等二进制数据,也可以上传表单键值对,只是最后会转化为一条信息;

x-www-form-urlencoded:只能上传键值对,并且键值对都是间隔分开的。


raw 原始数据

  • Content-Type:application/json
  • Content-Type:text/plain
  • Content-Type:application/xml
  • Content-Type:text/xml
  • Content-Type:text/html


binary

  • Content-Type:application/octet-stream

binary 这种方式我用的不多,它一次只能传一个文件。





@RequestBody 也可以用 GET 方式,只要你的 GET 方式中有消息体传过来。


很多人讨论GET和POST的时候很容易就从“协议”讨论到“实现”上去了。 协议里说的是,GET是从服务器取回数据,POST是发送数据,HTTP请求有header,有body。但是实现怎么样,协议就不管了。本文章的HTTPClient,curl,postman,浏览器,这些都是实现。

所以我觉得讨论GET和POST区别的前提是,弄明白协议和实现的区别。比如别人可以说对于Chrome,get的区别是不能带body,这就没问题了。

然而协议里确实说了哪些方法带body是没有意义并可能会产生问题,所以你硬给get加一个body也是属于不遵循规范的,尽管可能成功但是后果自担。所以说get不能带body是正确的说法

A message-body MUST NOT be included in
a request if the specification of the request method (section 5.1.1)
does not allow sending an entity-body in requests. A server SHOULD
read and forward a message-body on any request; if the request method
does not include defined semantics for an entity-body, then the
message-body SHOULD be ignored when handling the request.

这是 RFC 中 message-body 中的描述,但是没有说 GET 不能携带 body,看到 HEAD 和 OPTIONS 忽略 body

本文并没有建议去违反语义在 GET 请求中传递 body。由于 HTTP/1.1 是基于文本的协议,所以头后空一行后的数据都是 body,所以协议本身未作限制,但是有一个语义上的建议--不应在 GET 请求中放 body

这里有个比较好的回答
https://stackoverflow.com/questions/978061/http-get-with-request-body?answertab=active#tab-top

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress).

....Roy

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in the HTTP/1.1 spec, section 4.3:

[...] if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

And the description of the GET method in the HTTP/1.1 spec, section 9.3:

The GET method means retrieve whatever information ([...]) is identified by the Request-URI.

which states that the request-body is not part of the identification of the resource in a GET request, only the request URI.

https://blog.csdn.net/qq_28411869/article/details/81285810

关于 GET、POST、表单、Content-Type

标签:pcl   ipa   标签   form 表单   headers   anti   消息   区别   bin   

原文地址:https://www.cnblogs.com/tuhooo/p/10717558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!