关于 GET、POST、表单、Content-Type
关于 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的更多相关文章
- form 表单<input type="button" value="登录" onclick="loginSubmit ()"/> 点击提示 Uncaught TypeError: loginSubmit is not a function
在网上搜了一堆东东,仔细看了一下,再加上实验,发现原因出在<form>中. <form method="post"> <button type=&qu ...
- 关于在用curl函数post网页数据时,遇上表单提交 type为submit 类型而且没有name和id时可能遇到的问题及其解决方法
curl函数库实现爬网页内容的链接在 http://www.cnblogs.com/linguanh/p/4292316.html 下面这个是没有name和id 标识的 <input type= ...
- input表单的type属性详解,不同type不同属性之间区别
目标:详解表单input标签type属性常用的属性值 一.input标签和它的type属性 PS:input 元素可以用来生成一个供用户输入数据的简单文本框. 在默认的情况下, 什么样的数据均可以输入 ...
- HTML5的表单所有type类型
1.button:定义可点击的按钮(通常与 JavaScript 一起使用来启动脚本).<br /> <input id="" type="button ...
- form表单提交 type="submit"
<form action="" method="post" onsubmit="return validte()"> < ...
- form表单提交图片禁止跳转
问题: 最近在做项目时,遇到上传图片需求,且在不跳转的情况下获取到返回信息 思路: 1. 使用ajax发送异步请求,经多次测试,最终以失败告终 2. iframe 禁止跳转(未尝试) 3. 修改fo ...
- Django学习笔记(6)——Form表单
知识储备:HTML表单form学习 表单,在前端页面中属于最常见的一个东西了.基本上网站信息的提交都用到了表单,所以下面来学习Django中优雅的表单系统:Form 表单的主要作用是在网页上提供一个图 ...
- Ajax提交表单初接触
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...
- 前端 HTML body标签相关内容 常用标签 表单标签 form里面的 input标签介绍
input标签用于接收用户输入.可以利用input 可以做登录页面 input标签是行内块标签 <input> 元素会根据不同的 type 属性,变化为多种形态. name属性:表单点击提 ...
- jQuery html5Validate基于HTML5表单验证插件
更新于2016-02-25 前面提到的新版目前线上已经可以访问: http://mp.gtimg.cn/old_mp/assets/js/common/ui/Validate.js demo体验狠狠地 ...
随机推荐
- Vue渲染列表,在更新data属性后,列表未更新问题
初始化代码如下var tableModel = new Vue({el: '#datatable',data: {items: listData}}); 当我对列表操作时,listData=>n ...
- winfrom datagridview中DataGridViewTextBoxColumn的联动处理
这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件 方法一: 首先 窗体上拖拽一个 DataG ...
- SQL总结——存储过程
SQL总结(五)存储过程 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存储过程语法 CREATE proc | procedure pro ...
- IDEA安装使用Lombok插件
项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然IDEA开发环境下都有自动生成的快捷方式,但自动生成这些代码 ...
- IntelliJ IDEA配置Springboot2.x 通过devtools实现代码热部署,提高调试效率
1.pom.xml添加依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- CCNA
- 可以直接用的“ html转字符串string”方法
//html转字符串 -(NSString *)filterHTMLString:(NSString *)html { NSScanner * scanner = [NSScanner scanner ...
- helm-chart-1-简单概念介绍-仓库搭建-在rancher上的使用
简单的概念介绍: Chart是helm管理的应用的打包格式,一个chart对应一个或一套应用.内部是一系列的yaml描述文件,以为为yaml 服务的文件. 三个部分,helm .tiller.repo ...
- Vue.JS React 精彩文章汇总
JavaScript深入系列 [干货] JavaScript数组所有API全解密 [干货] 移动端:页面->手淘互动动效的探索 - IT大咖说 - 大咖干货,不再错过 [扫盲] Jonath ...
- Python3 与 C# 并发编程之~ Net篇
NetCore并发编程 示例代码:https://github.com/lotapp/BaseCode/tree/master/netcore/4_Concurrency 先简单说下概念(其实之前也有 ...