Understanding REST API

in #utopian-io7 years ago (edited)

What Will I Learn?

  • The Concept of API
  • API Request Types
  • JSON

Difficulty

  • Basic
  • Intermediate

Tutorial Contents

What is a REST API? What can it do for you? How do you use it?
In this article, you’ll learn everything you need to know about REST APIs to be able to read API documentations and use them effectively.

What is a REST API

An #API is an application programming interface. It is a set of rules that allow programs to talk to each other. The developer creates the API on the server and allows the client to talk to it.

#REST determines how the API looks like. It stands for “Representational State Transfer”. It is a set of rules that developers follow when they create their API. One of these rules states that you should be able to get a piece of data (called a resource) when you link to a specific URL.

Each URL is called a request while the data sent back to you is called a response.

JSON

JSON (JavaScript Object Notation) a common format for sending and requesting data through a REST API. The response that Github sends back to you is also formatted as JSON.

A #JSON object looks like a JavaScript Object. In JSON, each property and value must be wrapped with double quotation marks, like this:

{ "property1": "value1", "property2": "value2", "property3": "value3" }

The Anatomy Of A Request

A request consists of four parts.

  • The endpoint
  • The method
    -The headers
    -The data (or body)

Let’s go through what makes up a request.

The Method

The method is the type of request you send to the server. You can choose from these five types below:

*GET
*POST
*PUT
*PATCH
*DELETE

These methods provide meaning for the request you’re making. They are used to perform four possible actions: Create, Read, Update and Delete (CRUD).

GET -- This request is used to get a resource from a server. If you perform a GET request, the server looks for the data you requested and sends it back to you. In other words, a GET request performs a READ operation. This is the default request method.

POST -- This request is used to create a new resource on a server. If you perform a POST request, the server creates a new entry in the database and tells you whether the creation is successful. In other words, a POST request performs an CREATE operation

PUT and PATCH -- These two requests are used to update a resource on a server. If you perform a PUT or PATCH request, the server updates an entry in the database and tells you whether the update is successful. In other words, a PUT or PATCH request performs an UPDATE operation.

DELETE -- This request is used to delete a resource from a server. If you perform a DELETE request, the server deletes an entry in the database and tells you whether the deletion is successful. In other words, a DELETE request performs a DELETE operation.

HTTP Status Codes And Error Messages

Link

Some of the messages you’ve received earlier, like “Requires authentication” and “Problems parsing JSON” are error messages. They only appear when something is wrong with your request. HTTP status codes let you tell the status of the response quickly. The range from 100+ to 500+. In general, the numbers follow the following rules:

200+ means the request has succeeded.

300+ means the request is redirected to another URL

400+ means an error that originates from the client has occurred

500+ means an error that originates from the server has occurred



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.smashingmagazine.com/2018/01/understanding-using-rest-api/

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

Plagiarised from here.

You can contact us on Discord.
[utopian-moderator]