The REST API is a web programming interface that follows the REST application architecture style. The REST API allows communication between applications over the internet/intranet using standard HTTP protocols, such as GET, POST, PUT, and DELETE.
Golang has a built-in standard library/package named 'net' that is essential for web applications and Web APIs. The 'net' package includes several standard packages to work on network-related tasks. These packages provide the functionality needed for network-related projects. Some of them are:HTTP: to work with HTTP client and server implementation.
mail: to work with mail messages
httptest: for testing the web application
URL: to work with URL parsing and implementation
textproto: to work with generic text-based request/response protocol.
Example: Continue from Part 9 of this series( Part 9 - Pointers and Their efficient uses )
In this example, we will create a simple REST API using Golang's net/http package. We will create a web server, run the web server on a specified port, and also create the index page of a web application and browse it in the browser.
Let's go through the step-by-step process of how to create a REST web API in Golang.
Step 1: Add a module/folder named rest_api under the project "learngo."
Step 2: Go to the rest_api directory in the terminal and run the below command to initiate the module:
$ go mod init rest_api
Step 3: Add a file named main.go under the folder rest_api and add the following code:{"message":"API invoked with GET method"}
API Frameworks
Golang has several API frameworks available for working with network applications. Gin, Echo, Gorilla Mux, Buffalo, and Goji are some of them. Below, we will use the Gin API framework to create a fully functioning API:Example: In this example, we will add the REST APIs to add, update, and fetch the name and address information in the project we are working on in this learning series.
Step 1: Add gin HTTP web framework package to the project. To add the package run the below command to add the gin package to your project
$go get github.com/gin-gonic/gin
Step 2: Add a folder named 'handlers' under the package rest_api and then add a file named 'address_handler.go'
Copy the below code into address_handler.go file
Step 3: Modify the main.go to the file under the rest_api folder and add the below code to use the handler to handle HTTP requests.
Step 4: Make sure to run the command to add package dependencies in the go.mod file
$go mod tidy
After running this command the go.mod file should look like this:
Step 5: Now, build and run the project, it will start the HTTP server and listen to any HTTP requests on port no 8080
Step 6: Open a browser window and browse the URL "http://localhost:8080/address"
The browser will display below address data:
[ { "HouseNumber": "1234", "StreetName": "abc Pkwy", "City": "Frederick", "State": "MD", "ZipCode": "21000" }, { "HouseNumber": "5678", "StreetName": "xyz lane", "City": "Silver Spring", "State": "MD", "ZipCode": "22000" } ]
Alternatively, we can run the below command to invoke the webAPI in the terminal/command prompt:
S******s-MacBook-Pro:~ s*********i$ curl http://localhost:8080/address
[
{
"HouseNumber": "1234",
"StreetName": "abc Pkwy",
"City": "Frederick",
"State": "MD",
"ZipCode": "21000"
},
{
"HouseNumber": "5678",
"StreetName": "xyz lane",
"City": "Silver Spring",
"State": "MD",
"ZipCode": "22000"
}
]
Now, we have added a REST API using the Gin framework and called it using the command prompt and web browser. We can use tools like Postman and Fiddler to test the rest API.
Consume the REST API
Now, in this section, I will walk you through how to use the recently created REST API to perform operations on different applications.Golang has a very simple and straightforward process to consume REST APIs compared to other programming languages. We just need to use the inbuilt net/http package to invoke the REST service.
Step 1: Modify main.go and update the code to call the address API. The code below replaces the previous one to read the response directly using the stringformatter package.
Step 3: Open the new terminal n vs code and run the module rest_api. The module starts the HTTP web server and waits for the HTTP requests.
Step 4: Open another terminal in vs code and run the main. go module. you will see the address data from the database in the terminal console.
Complete code: https://github.com/learnwithsharad/learngo/tree/shaad-RestAPI
Reference
https://pkg.go.dev/net/http#HandlerFunc
https://go.dev/doc/tutorial/web-service-gin
https://gin-gonic.com/docs/examples/bind-uri/
No comments:
Post a Comment