关于 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体验狠狠地 ...
随机推荐
- Codeforces.1051G.Distinctification(线段树合并 并查集)
题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在 ...
- koa2框架设置响应和请求头
https://koa.bootcss.com/#response 请耐心翻到网页下端,可以看到 设置响应头: ctx.set('Content-Type', 'application/zip') 添 ...
- React入门——制作一个TodoList App
源码 import React, { Component, Fragment } from "react"; class TodoList extends Component { ...
- .net 后台获取CheckBoxList的值
版权声明:本文档可以随意转载,我愿意和大家共同分享,共同进步. http://blog.csdn.net/u014479921/article/details/73649799 1.前台的多选控件 & ...
- django生产环节部署
在linux下安装mysql yum install mysql-server mysql -u root(安装完后,你的root账户是没有密码的.所以你可以直接使用这条命令,就可以登陆控制台了) 如 ...
- JS_高程3.基本概念(5)语句
1.if语句 2.do-while语句:后测循环语句,循环体内的代码至少执行一次. 3.while语句:前测循环语句. 4.for语句:前测循环语句. 注意:在ECMAScript中不存在块级作用域, ...
- web端设计和web前端开发 的区别
- F - 质数检测 V2
https://vjudge.net/contest/218366 Java解 import java.math.BigInteger; import java.util.Scanner; publi ...
- pygame-KidsCanCode系列jumpy-part4-弹跳
终于要到弹跳环节了,向上弹跳其实很简单,按下空格触发时,只要把y轴速度给一个向上的速度即可. Player类,新加一个jump()方法: def jump(self): self.vel.y = -2 ...
- Firefly-RK3399笔记
有用的连接: Rockchip官方资料: http://opensource.rock-chips.com/wiki_Main_Page http://opensource.rock-chips.co ...