关于 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. 深入理解原型链(Prototype chain) __proto__

    原型链(Prototype chain) 原型对象也是普通的对象,并且也有可能有自己的原型,如果一个原型对象的原型不为null的话,我们就称之为原型链(prototype chain). A prot ...

  2. 如何在ElementUI中的Table控件中使用拼音进行排序

    本人使用版本是1.4.7 在这个版本中对应全是String的column进行排序并不是按照拼音的方式排列的. 这里我贴一下源代码就可以看出是为什么了: export const orderBy = f ...

  3. Java -- 内部类(二)

    在上一篇博客Java --内部类(一)中已经提过了,java中的内部类主要有四种:成员内部类.局部内部类.匿名内部类.静态内部类. 该文主要介绍这几种内部类. 成员内部类 成员内部类也是最普通的内部类 ...

  4. 关于git的使用记录总结

    1.解决Windows下git换行符报警问题 git config --global core.autocrlf false 2.撤销add的文件退出暂存区 git reset --mixed 3.g ...

  5. redis(六)

    安装包 到中文官网查找客户端代码 联网安装 sudo pip install redis 使用源码安装 unzip redis-py-master.zip cd redis-py-master sud ...

  6. Perfect Service [POJ 3398]

    Perfect Service 描述 网络由N个通过N-1个通信链路连接的计算机组成,使得任何两台计算机可以通过独特的路由进行通信.如果两台计算机之间存在通信链路,则称这两台计算机是相邻的.计算机的邻 ...

  7. Unity 显示FPS(C#语言)

    直接上脚本了: using UnityEngine; using System.Collections; public class ShowFPS : MonoBehaviour { //设置帧率 A ...

  8. linux常用命令和关闭防火墙

    linux常用命令和关闭防火墙   2.        linux常用命令 Mkdir 创建 Rm -rf 删除 Chmod -R 777 权限 Mysql -uroot -r quit退出 find ...

  9. 浏览器模式&用户代理字符串(IE)

    问题问题描述今天在做项目的时候,QA部门提了一个Bug,在一个搜索列表中,搜索栏为空时刷新页面,却触发了搜索功能,并且列表显示出<未搜索到结果> 环境IE11 问题原因 QA的IE11用户 ...

  10. javagc日志详解

    https://blog.csdn.net/aliveTime https://plumbr.io/blog/garbage-collection/understanding-garbage-coll ...