If you are new to creating RESTful APIs in Go, it is better to learn things from scratch. In this blog, we start right from the REST architecture and get an introduction to JSON. When you have a basic understanding, it will become easier to use CRUD operations. So, without further ado, let’s get started.
What is REST Architecture?
- REST stands for REpresentational State Transfer. It refers to an architectural style and is not a specification. This means that it does not represent any formal specification/document that would corroborate whether you have created a RESTful API or not, on the back-end.
- It comprises status codes, HTTP methods and response types. Some of the most important HTTP methods have been described belowm.
|GET||You use this method when you wish to get something from the server |
|POST ||Sending a POST request signifies that you wish to create a resource |
|PUT||The PUT method replaces the resource with one being forwarded in the body of the client |
|DELETE ||This method is applied to delete the resource |
Table 1: Important HTTP Methods
- The status codes or the HTTP protocol in 100 to 199 signifies informational response. Successful responses are denoted by status codes of 200 to 299. Status codes with 300 to 399 tells us that redirects are happening.
What is a REST API?
Now that you have understood what REST architecture is, it is time to focus on REST API. Now, you will find several blog posts and tutorials that delve into the technical aspect. But, how do you explain it to a beginner?
- Well, consider this. Most applications in today’s world use client-server architecture. Imagine a weather app. The one you get to see is the front-end, or the client side. Now, if you wish to check the weather report of Arizona or New Delhi, how do you think you are going to receive the data?
- Well, for this, you can count your lucky stars, as there are several web services (dynamic servers or Cloud) available. What you have to do is send a request to the server to get the data. And the communication happens via HTTP protocols.
Source: What is REST API | PHPenthusiast
- What the server usually returns is web pages, i.e, HTML pages. But you want data, and not some designs, right? So, the back-end server now sends you data in the form of XML or JSON (that has a particular structure). But this leads to lots of overheads, where you have to send requests, the server has to respond.
- What if there is a simple solution for all of this? That is the RESTful API. If the server has to return some data, then you create an object (on the server) and return the value of the object. So, essentially, the server is sending the ‘state’ of an object (resource). Now, this state of the object will be returned in the JSON or XML format.
- Now, the REST API works in the form of CRUD operations. To create, we use POST, for reading, we use GET. If we wish to update, then we use PUT and for deletion, we use DELETE.
- Now, questions might arise, how to implement the REST? Well, for this, we can use Jersey or Sparing.
What is JSON?
Next, we take a look at JSON.
- What usually happens is that objects are converted to JSON on the server side, and then JSON is converted to objects on the client side. And this is achievable with the help of libraries, like GSON, JACKSON.
- Now, you may have heard the term JSON Schema. Let me explain to you what it is in simple words. Well, JSON Schema (which may be a format/structure/document) is used for the validation and formatting of the JSON message or document.
- With JSON Schema, you can modify the data types, structure and content, and syntax of the JSON message. It is also used to validate the API request and response.
Hopefully, you have a perfect understanding of the different concepts. It will help you delve right into the programming aspect. And you will be able to code easily. If you have further doubts, it is always helpful to study online, with a myriad of resources available at the click of a button.