Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Explore the world of REST API! Learn what REST APIs are, how they work, and their role in modern software development and integration.
REST API (Representational State Transfer) is an architectural style for designing networked applications. It uses HTTP to perform CRUD (Create, Read, Update, and Delete) operations on resources, which are usually represented as JSON or XML objects.
Also Read: What is an API?
REST APIs use Uniform Resource Identifiers (URIs) to identify and access resources. For example, a blog API could have the following URIs:
/posts (represents all blog posts)
/posts/1 (represents a specific post with ID 1)
Also Read: Standard Laravel Responses
https://api.twt.comhttps://api.twt.com/feed?type=posts&published=truewe add the ? in URL, followed by item which we want to search
Here the client communicates to the server that the request is to retrieve products with type equals posts and published equals true.
URI Example: https://api.twt.com/posts
Here the URL is: https://api.twt.com
and URI is: /posts
Therefore, every URL is an URI, but not all URIs are URLs.
For Example:
{
"category": "web",
"published":true,
"no_of_posts":10
}https://api.twt.com/posts/category/web, here web could be the path parameter that is a variable and could be anything, so this API responds with all posts in web category.Also Read: Rest API in PHP
Authorization: Bearer {token}
Accept: application/json
Content-Type: application/jsonIn the above example, the client is sending headers to request response in desired format and passing in authorization token to get the results.
RESTful APIs use standard HTTP methods to perform operations on resources:
GET: Retrieve a resource or a collection of resources. For example, GET /posts retrieves all blog posts, while GET /posts/1 retrieves the post with ID 1.
POST: Create a new resource. For example, POST /posts with a JSON payload creates a new blog post.
PUT or PATCH: Update an existing resource. For example, PUT /posts/1 or PATCH /posts/1 with a JSON payload updates the blog post with ID 1.
DELETE: Remove a resource. For example, DELETE /posts/1 deletes the blog post with ID 1.
REST APIs are stateless, meaning that each request must contain all necessary information for the server to process it. Servers don't store any information about the client's state between requests.
RESTful APIs can leverage caching to improve performance by storing a copy of a previously retrieved resource and using it for subsequent requests, reducing server load.
Clients and servers can negotiate the format of the data exchanged, typically using the Accept and Content-Type headers. Common formats are JSON and XML.
HTTP status codes are three-digit numbers that indicate the outcome of an HTTP request. They help clients understand if the request was successful or if any issues occurred during the processing. Status codes are grouped into five classes based on the first digit:
Also Read: Handle CORS errors
Here are some common status codes used in REST APIs:
Also Read: Build API with Laravel
These status codes help REST API clients handle responses more effectively by understanding the outcome of their requests and taking appropriate actions based on the status codes received.