关于 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的更多相关文章

  1. form 表单<input type="button" value="登录" onclick="loginSubmit ()"/> 点击提示 Uncaught TypeError: loginSubmit is not a function

    在网上搜了一堆东东,仔细看了一下,再加上实验,发现原因出在<form>中. <form method="post"> <button type=&qu ...

  2. 关于在用curl函数post网页数据时,遇上表单提交 type为submit 类型而且没有name和id时可能遇到的问题及其解决方法

    curl函数库实现爬网页内容的链接在 http://www.cnblogs.com/linguanh/p/4292316.html 下面这个是没有name和id 标识的 <input type= ...

  3. input表单的type属性详解,不同type不同属性之间区别

    目标:详解表单input标签type属性常用的属性值 一.input标签和它的type属性 PS:input 元素可以用来生成一个供用户输入数据的简单文本框. 在默认的情况下, 什么样的数据均可以输入 ...

  4. HTML5的表单所有type类型

    1.button:定义可点击的按钮(通常与 JavaScript 一起使用来启动脚本).<br /> <input id="" type="button ...

  5. form表单提交 type="submit"

    <form action=""  method="post" onsubmit="return validte()"> < ...

  6. form表单提交图片禁止跳转

    问题: 最近在做项目时,遇到上传图片需求,且在不跳转的情况下获取到返回信息 思路: 1.  使用ajax发送异步请求,经多次测试,最终以失败告终 2. iframe 禁止跳转(未尝试) 3. 修改fo ...

  7. Django学习笔记(6)——Form表单

    知识储备:HTML表单form学习 表单,在前端页面中属于最常见的一个东西了.基本上网站信息的提交都用到了表单,所以下面来学习Django中优雅的表单系统:Form 表单的主要作用是在网页上提供一个图 ...

  8. Ajax提交表单初接触

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  9. 前端 HTML body标签相关内容 常用标签 表单标签 form里面的 input标签介绍

    input标签用于接收用户输入.可以利用input 可以做登录页面 input标签是行内块标签 <input> 元素会根据不同的 type 属性,变化为多种形态. name属性:表单点击提 ...

  10. jQuery html5Validate基于HTML5表单验证插件

    更新于2016-02-25 前面提到的新版目前线上已经可以访问: http://mp.gtimg.cn/old_mp/assets/js/common/ui/Validate.js demo体验狠狠地 ...

随机推荐

  1. 码云,git使用 教程

    码云,git使用 教程 code cloud, git use tutorials 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail ...

  2. Yahoo Programming Contest 2019.D.Ears(DP)

    题目链接 菜爆了啊QAQ 记起点为\(S\),终点为\(T\),走过的最靠左的点是\(L\),最靠右的点是\(R\). 那么坐标轴被分成了五段: \(0\sim L-1\):经过\(0\)次: \(L ...

  3. MySql中的事务、JDBC事务、事务隔离级别

    一.MySql事务 之前在Oracle中已经学习过事务了,这个东西就是这个东西,但是在MySql中用法还是有一点不同,正好再次回顾一下. 先看看MySql中的事务,默认情况下,每执行一条SQL语句,都 ...

  4. [P1082][NOIP2012] 同余方程 (扩展欧几里得/乘法逆元)

    最近想学数论 刚好今天(初赛上午)智推了一个数论题 我屁颠屁颠地去学了乘法逆元 然后水掉了P3811 和 P2613 (zcy吊打集训队!)(逃 然后才开始做这题. 乘法逆元 乘法逆元的思路大致就是a ...

  5. Flask实例化配置

    template_folder :指定模板存放路径,默认值:temolates from flask import Flask, url_for app = Flask(__name__,templa ...

  6. 小甲鱼Python第十四课后习题

    字符串格式化符号含义    符   号    说     明      %c    格式化字符及其ASCII码[>>> '%c' %97        'a']      %s    ...

  7. Groovy 反射字符串常量方法

    Keywords: Groovy, Reflection, 反射 The Reflection of Groovy String constant style method. Groovy支持以下的方 ...

  8. Go语言二叉树定义及遍历算法实现

    // binary_tree 二叉树 package Algorithm import ( "reflect" ) // 二叉树定义 type BinaryTree struct ...

  9. JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构

    一.简介 JAVA中调用LevelDB用于Linux和Window环境下快速存储KV结构 二.依赖 <!-- https://mvnrepository.com/artifact/org.fus ...

  10. TLB的作用及工作原理

    TLB的作用及工作过程 以下内容摘自<步步惊芯——软核处理器内部设计分析>一书 页表一般都很大,并且存放在内存中,所以处理器引入MMU后,读取指令.数据需要访问两次内存:首先通过查询页表得 ...