What is an API?
API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Thus, there are many ways in which we can "talk" to the applications and these ways are called methods of API.
Prerequisites
- We will use an awesome tool called Postman for testing these methods of API. You can download it from here.
List of Methods
- GET
- POST
- PUT
- PATCH
- DELETE
GET Method
The name suggests it all, it is used to request data from a specified resource.
Example
HTTP GET https://jsonplaceholder.typicode.com/posts/1
POST Method
This method is used to send data to a server to create/update a resource. People get often confused between PUT and POST, which we will discuss in the later section.
Example
HTTP POST https://jsonplaceholder.typicode.com/posts
BODY
[{
title: 'foo',
body: 'bar',
userId: 1,
}]
PUT Method
This method is also used to send data to a server to create/update a resource. But, it is different from POST in the following ways:
- PUT requests are idempotent i.e. calling the same PUT request multiple times will always produce the same result because if you invoke a PUT API N times, the very first request will update the resource; the other N-1 requests will just overwrite the same resource state again and again – effectively not changing anything
- In contrast, calling a POST request repeatedly have the side effects of creating the same resource multiple times.
Example
HTTP PUT https://jsonplaceholder.typicode.com/posts/1
BODY
[{
id: 1,
title: 'foo',
body: 'bar',
userId: 1,
}]
PATCH Method
This method is used to make a partial update on a resource.
Now, what partial means is that in the PUT method it modifies a resource entity, whereas it will partially (only some properties) modify the resource.
Example
HTTP PATCH https://jsonplaceholder.typicode.com/posts/1
BODY
[{
title: 'foo',
}]
DELETE Method
The name itself specifies that it used to delete the resources.
- DELETE operations are idempotent.
Example
DELETE https://jsonplaceholder.typicode.com/posts/1