Building Web APIs in Go
Understand what a web API is and how to write one in Go.
A web API is a service that provides access to some resources that can be consumed over the internet. Usually, the consumer is a client application, like a JavaScript application that lives within the browser, while the producer is an application that serves resources in a controlled way. This is why we talk about application-to-application communication. The de facto output of a web API is in JSON, though some APIs still reply with XML or other kinds of data. In addition, they don’t respond with HTML tags as they don’t provide any visual content to the caller. That’s why web APIs are meant to be contacted by applications that parse the data retrieved to produce visual content for the final user.
Web API
Now, let’s visualize the different actors of web communications:
In the previous diagram, we see the various components that can take a role in client-server communication. They generally consist of:
- Server: A web API application that can interact with databases and internal services, among others.
- Clients: A website, mobile application, desktop application, and so on.
With that being said, let’s see the various tasks of a web API.
HTTP protocol
The communication between the client and the server is done through the HTTP protocol. The client sends an HTTP request, and the server replies with an HTTP response. Let’s ...