`

The Definitive Guide to GET vs POST

阅读更多

Note: Remember that query strings (i.e. name/value pairs) get transferred in the URL of GET requests:

GET /blog/?name1=value1&name2=value2 HTTP/1.1
Host: carsonified.com

and in the body of POST requests:

POST /blog/ HTTP/1.1
Host: carsonified.com
name1=value1&name2=value2

GET vs POST Basics

In between new additions to our vocabularies (think “idempotent“), sections 9.1, 9.3 & 9.5 of RFC 2616 help us to conclude the first rule of GET vs POST…

Rule #1: Use GET for safe actions and POST for unsafe actions.

The RFC instructs internet browsers to make users aware that, when reissuing a previously made POST request, that the action (e.g. placing an order) is potentially unsafe. Hence the existence of dialogue boxes like this:

However, whilst browser compliance with this RFC instruction might explain why POST should be used for unsafe actions, why shouldn’t we use POST for safe ones?

Simply put, because GET requests are more useable:

  1. GET requests can be cached
  2. GET requests can remain in the browser history
  3. GET requests can be bookmarked
  4. GET requests can be distributed & shared
  5. GET requests can be hacked (ask Jakob!)

Note: If you need the best of both worlds, an unsafe action can be made safe by making it idempotent, so that it makes no difference how many times it’s requested. You do this by giving the request a unique ID and using server-side validation to ensure that a request with that ID hasn’t already been processed. In fact, if you’re in search of excellence, all unsafe actions should be made idempotent as nothing can stop users from ignoring warnings.

GET vs POST Extended

Rule #2: Use POST when dealing with sensitive data.

Because query strings are transferred openly in GET requests, we have to consider our security and that of our users when dealing with sensitive data like passwords or credit card numbers:

  1. Our users… because they may not realise that they are sharing sensitive data when they share a URL or that it can be viewed in the browser history by other people using the same computer.*
  2. Ourselves… because we may be breaking laws by unexpectedly storing data that we’re not allowed to (like credit card CV2s) in log files.

* This doesn’t apply when working within an AJAX environment.

Rule #3: Use POST when dealing with long requests.

Although the RFC doesn’t lay down any length-related guidelines, Internet Explorer – with its insistence on finding ways to make things difficult for us – enforces a maximum URL length of 2,048 characters.

Rule #4: Use GET in AJAX environments.

When using XMLHttpRequest, browsers implement POST as a two-step process (sending the headers first and then the data). This means that GET requests are more responsive – something you need in AJAX environments.

Summary

Although rules usually exist for good reasons, it’s good to know the logic behind them so they can be embraced fully. I, myself, hate rules that don’t have explanations and I hope that the above helps to justify the rules of GET vs POST.

Choosing between methods is a systematic process which should be part of second nature.

分享到:
评论

相关推荐

    HTTP: The Definitive Guide

    HTTP/1.0是HTTP协议的早期版本,它定义了基本的请求方法如GET、POST和HEAD,以及响应状态码。然而,随着互联网的发展,HTTP/1.0的一些限制逐渐显现,比如对并发连接的处理不理想,这导致了HTTP/1.1的诞生。 ...

    HTTP The Definitive Guide.rar

    GET用于获取资源,POST用于提交数据,PUT用于更新资源,DELETE用于删除资源。 3. **HTTP状态码**:HTTP响应中包含一个三位数字的状态码,如200(成功)、404(未找到)、500(服务器内部错误)等,这些状态码反映了...

    HTTP协议 - HTTP The Definitive Guide

    ### HTTP协议 - HTTP The Definitive Guide #### Part I. HTTP: The Web’s Foundation **1. Overview of HTTP** - **HTTP: The Internet’s Multimedia Courier** - **定义与作用:** HTTP(Hypertext Transfer...

    HTTP, The Definitive Guide

    GET用于获取资源,POST用于提交数据,PUT用于更新资源,DELETE用于删除资源。书中详细解释了这些方法的使用场景和差异,并讨论了安全性和幂等性原则。 HTTP状态码是服务器向客户端反馈请求结果的重要方式,如200...

    O'Reilly - HTTP - The Definitive Guide.zip

    《HTTP - The Definitive Guide》是O'Reilly出版社出版的一本权威性的HTTP协议指南,旨在深入解析HTTP(超文本传输协议)的工作原理及其在互联网中的应用。这本书涵盖了HTTP协议的基础概念、工作流程、请求与响应...

    Ajax The Definitive Guide

    1. **jQuery和库**:jQuery简化了Ajax操作,提供了如`$.ajax()`, `$.get()`, `$.post()`等函数。还有其他库如axios、fetch API等,它们都提供了更现代和便捷的方式来处理Ajax请求。 2. **跨域请求**:由于同源策略...

    The.Definitive.Guide.to.Django.Dec.2007.eBook-BBL\Apress.The.Definitive.Guide.to.Django

    本书《The Definitive Guide to Django》提供了全面深入的指导,帮助读者掌握Django的核心概念和技术细节。 #### 二、Django的特点 ##### 1. 快速开发 Django提供了一系列高级工具和功能,使得开发者能够快速地...

    HTTP.The.Definitive.Guide.zip_HTTP_HTTP协议

    《HTTP: The Definitive Guide》是一本深入探讨HTTP协议的权威指南,它涵盖了从基础概念到高级特性的全方位解析。 在HTTP协议中,最基本的概念是请求和响应。请求由客户端发起,通常包含方法(如GET、POST、PUT、...

    Dojo.The.Definitive.Guide

    《Dojo: The Definitive Guide》是一本深入讲解Dojo JavaScript库的权威指南,适合对Web开发有热情,特别是对JavaScript框架感兴趣的开发者。Dojo是一个功能强大的JavaScript工具集,旨在帮助开发者创建交互式和高...

    HTTP.The.Definitive.Guide

    其中,GET方法用于获取资源,POST方法常用于提交数据,PUT用于更新已有资源,而DELETE则用于删除资源。 HTTP响应也包括几个部分:状态码、原因短语、HTTP版本、头部和响应体。状态码是一个三位数字,如200表示成功...

    HTTP.The.Definitive.Guide.2002

    - **请求行**:包含方法(如GET、POST等)、资源路径和使用的HTTP版本。 - **状态行**:对于响应消息来说,包含HTTP版本、状态码和状态信息。 - **首部字段**:包含一系列键值对,用于传递关于消息的额外信息。 - **...

    Ajax-Basics-2.pdf

    例如,《Ajax in Action》、《Professional Ajax》和《Ajax: The Definitive Guide》等都是值得推荐的资源。这些书籍不仅提供了理论知识,还包含了大量实用的示例代码,有助于读者快速上手并掌握Ajax技术。 总之,...

    全文检索必备:一些常用的ElasticSearch查询语句

    { "title": "Elasticsearch: The Definitive Guide", "authors": ["clinton gormley", "zachary tong"], "summary" : "A distributed real-time search and analytics engine", "publish_date" : "2015-02-07", "num...

    AJAX开发简略-配有相关源代码-网络分享

    - "AJAX: The Definitive Guide" by David Flanagan - MDN Web Docs上的XMLHttpRequest教程:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest - W3Schools的AJAX教程:...

    Elasticsearch 一个开源的搜索和数据分析引擎

    - 《Elasticsearch: The Definitive Guide》 - 《Elasticsearch: Real-time Distributed Search and Analytics》 ### 总结 Elasticsearch 作为一款功能强大且灵活的搜索和数据分析引擎,已经在多个行业中得到了...

    elasticsearch数据库下载以及 配置、使用案例,项目代码

    'title': 'Elasticsearch: The Definitive Guide', 'author': 'Clinton Gormley', 'summary': 'A comprehensive guide to Elasticsearch.' } res = es.index(index=index_name, id=1, document=doc) print(f'...

    jquery帮助文档

    "JavaScript.The.Definitive.Guide,5th.Edition.chm"涵盖了JavaScript的核心概念,包括: - **变量与数据类型**:了解JavaScript的动态类型系统,如字符串、数字、布尔值、null、undefined等。 - **函数与作用域*...

Global site tag (gtag.js) - Google Analytics