# RESTful - What Are Idempotent and Safe Methods and How to Use Them?

DevFeed: [RESTful - What Are Idempotent and Safe Methods and How to Use Them?](<https://devfeed.tech/articles/restful-what-are-idempotent-and-safe-methods-and-how-to-use-them-24955.md>)

Original publisher: [Read original article](<https://codeahoy.com/2016/07/04/rest-design-choosing-the-right-http-method/>)

Author: umer

Published: 2016-07-04T00:00:00Z

Content type: tutorial

Language: en

Sources: [Code Ahoy - Articles](<https://devfeed.tech/sources/code-ahoy-articles.md>)

Topics: [REST API](<https://devfeed.tech/topics/rest-api.md>), [HTTP](<https://devfeed.tech/topics/http.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [http](<https://devfeed.tech/tags/http.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [rest-api](<https://devfeed.tech/tags/rest-api.md>)

## AI overview

A tutorial on choosing HTTP methods when designing REST APIs. It explains the safety and idempotency properties defined in the HTTP specification and describes how GET, POST, and PUT are used for retrieving, creating, and updating resources.

## Source excerpt

One of the challenges when designing a REST API is choosing the right HTTP method (GET, PUT, POST etc.) that corresponds with the operation being performed. Some people incorrectly assume that they can freely choose any method as long as the client and the server agree on it. This is wrong because a request passes through many intermediaries and middleware applications which perform optimizations based on the HTTP method type. These optimizations depend on two key characteristics of HTTP methods: idempotency and safety, which are defined in the HTTP specification. Safe HTTP Methods: Safe methods aren't expected to cause any side effects. These operations are read-only. E.g. querying a database. Idempotent HTTP Methods: Idempotent methods guarantee that repeating a request has the same effect as making the request once. Idempotency and safety are properties of HTTP methods that server applications must correctly implement. This means if you are implementing an operation and choose an idempotent HTTP method to invoke the operation, you must ensure that the implementation returns the same result if invoked once or multiple times for the same input. GET: Idempotent & Safe GET requests are used for retrieving information. These requests must be idempotent and safe: any operation invoked using GET must not alter the state of any resource. GET /books To get a specific book, GET /books/<title> POST: Non-Idempotent POST requests are not idempotent. They are used for creating new resources or updating existing ones. For example, suppose we have a resource called Student with the following attributes: name, college, major and gpa. To enroll a new student, we can use POST to create a new resource: POST /students/ // Create a new student { "name": "Michael Scarn", "college": "Stanford", "major": "computer science" } POST requests are allowed to perform partial updates. For example, to update the GPA of a student, we'll make the POST request on the specific record (given by the s