
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
Hello there! This article is very interesting but on the other hand more of non practical one. But tons of things to learn so stay tuned till last.
In this tutorial you will learn
For REST API Documentation please use this link Postman REST Demo Documentation Link. This is not the complete working code for this. In this demo there is no security for the API's. For securing API's I will cover in another article.
Once you open this Postman Documentation Link you can change to other languages too :)
HTTP
is an acronym of Hyper Text Transfer Protocol. This protocol is responsible for communication between the Server's & Client's.
Server's are the devices or computers that provides the requested data. Usually they host the website/software within them for Eg: facebook.com, google.com.
Client's are the devices or software within device, which is used to fetch the data from Server. Eg: Google Chrome, FireFox, Safari
The communication between the Server & Client is exchanged via HTTP Requests & HTTP Responses. When user from Client (Eg: browser - Google Chrome, Safari, Firefox) request for some data to Server (Eg: facebook.com/google.com), the Server returns with Data & Headers.
Every time while Client requesting data to server it tell what kind of data it needs along with headers. Based on the needs of the data from Client, Server responds with that data with headers
Each & every request are independent. Which usually we call as Stateless. Now days web applications uses sessions & cookies to remember users activity.
The famous HTTP methods used to exchange the data between the Client & the Server are as follows
NOTE: For all the requests I will give Command Line CURL
. If you click this Postman Documentation Link you can change to other languages too :)
When you want to only fetch the data from server use GET
request method. The server will send the requested data along with status codes.
GET request is less secure
Example:
Get list of all users
curl --location --request GET 'https://reqres.in/api/users?page=1&per_page=2'
Use POST
request method when you want to send the data to server, & server upon receiving this data saves the data to respective database.
Once the data is saved it returns data, headers & status codes.
POST request is more secure that GET request
Example:
New users registrations. Login Form & any other form data filling page.
curl --location --request POST 'https://reqres.in/api/users' \
--data-urlencode 'name=morpheus' \
--data-urlencode 'job=leader'
PUT
request is similar to POST
request and you can send data to server. But the only difference is PUT
is used to update the data which was saved earlier by POST
request.
When you want to replace the database table record and add new record while updating you will use PUT
request method.
Example:
User will have hobbies, when we update user hobbies instead of checking if the new updated hobby lists is added or not, if new hobby list exists or not, then adding it. This will be pretty much complex if we hit database for the same.
Instead we remove all the user hobbies first and then insert the new hobbies.
CURL REQUEST (Update User Details)
curl --location --request PUT 'https://reqres.in/api/users/266' \
--data-urlencode 'name=Channaveer' \
--data-urlencode 'job=Hakari'
PATCH
is similar to that of POST
request and you can send the data to server for updating the data.
When you want to patch the existing database record you use PATCH request method.
Example:
If you want to update the user details like password then you will only update that password, you don't want to replace the whole user record as user might be having other dependant details.
CURL REQUEST (Update User)
curl --location --request PATCH 'https://reqres.in/api/users/266' \
--data-urlencode 'name=Channaveer' \
--data-urlencode 'job=Hakari'
When you need to delete the resource from the database you will use DELETE
request method.
Example:
If you want to delete hobbies of user then you can delete it with this request method.
CURL REQUEST (Delete User)
curl --location --request DELETE 'https://reqres.in/api/users/266'
HEAD
request is same as that of GET
request method. But the only difference between GET
& HEAD
is HEAD
- Only return headers, where as GET
return data with headers.
So when you don't need the data from the server then you use this particular request method.
Example:
If you want check pings to your server then you don't want any return data. We call it as HeartBeat check
CURL REQUEST (Check If Server Is Live)
curl --location --request HEAD 'https://someurl.in/api/heartbeat'
You use UNLINK
request to remove any uploaded or particular path files. I basically use this for that.
Example:
If you wan to delete the obsolete invoice or database backup then you use this request method.
As I had explained you earlier in this article
As explained above
When you perform HTTP Requests & HTTP Responses you will come across weird numbers like 200, 201, 301, 302, 404, 400. What the heck is this? Sometimes you might even see weird 404 while browsing the web pages.
These are nothing but the HTTP status codes. To put it simple HTTP status codes are returned by the server when client request for data.
HTTP status codes are divided into 5 categories
1xx informational response – the request was received, continuing process
2xx successful – the request was successfully received, understood, and accepted
3xx redirection – further action needs to be taken in order to complete the request
4xx client error – the request contains bad syntax or cannot be fulfilled
5xx server error – the server failed to fulfil an apparently valid request
Source: Wikipedia
The most common HTTP status codes are as follows
200 (OK) - The server responds with 200 when the request data is found with the server. Eg: Get product details, get user details etc.
201 (Created) - When a new record is create on server side then it will return with 201 status code. Eg: New user registered, New bug created.
301 (Moved Permanently) - This HTTP is received when something your looking for is moved to other site or redirected permanently. Eg if big company acquires small companies then instead of closing acquired company website they will point it to theirs with this header.
When you try https://www/stackcoder.in
the it will redirect you to https://stackcoder.in
302 (Moved Temporarily) - This one you will get when you try to redirect from one path to another path in the same website. Eg: when you go for https://stackcoder.in
then you will be redirected to https://stackcoder.in/posts
.
400 (Bad Request) - If you don't pass the proper data to server then you will get validations errors, or else when you don't pass something which is required in server then you get 400 status.
401 (Unauthorised) - When you try to request the resource which you don't have permission. Eg: When you try to delete admin user being as normal client then you cant do it, there you will get this status error.
404 (Not Found) - If the page your looking out for is not found then you will get 404 status.
405 (Method Not Allowed) - When you try to POST the data for GET request or try to directly add POST URL in the web address bar then you will get this kind of error.
HTTP is like a sea. I have tried my best to cover very basics for you guys. I know I might have skipped a lot of things, but I will assure you guys that I will bounce back with continued follow articles for this.
Proper Way To Validate MIME Type Of Files While Handling File Uploads In PHP
PHP file_put_contents failed to open stream: Permission denied
Move Uploaded Files From Local Computer Or Server To Amazon S3 Bucket In PHP
Plain PHP Resumable Large File Uploads In Chunks Using FlowJs
Setup AMP (Accelerated Mobile Pages) In PHP Laravel
Send SMS With Amazon SNS (Simple Notification Service) Using PHP
Sass or SCSS @mixin vs @extends vs Placeholder (%)
Install Apache Web Server On Ubuntu 20.04 / Linux & Manage It
Accessors And Mutators In PHP Laravel
What Is Laravel Resourceful Controllers?
Factory States For Clean And Fluent Laravel Testing