Understanding HTTP Server in Go #6 - Mux

Up until now, we’ve been using the DefaultServeMux, but it’s time to wield more control and precision by explicitly using the http.ServeMux. Plus, we’ll look at the powerful frameworks available, like Gorilla Mux and Echo, that take web server functionality to new heights. Check out previous posts: Part 1: https://surajincloud.com/understanding-http-server-in-go-basic Part 2: https://surajincloud.com/understanding-http-server-in-go-using-httpserver-struct Part 3: https://surajincloud.com/understanding-http-server-in-go-handlers Part 4: https://surajincloud.com/understanding-http-server-in-go-multiple-handlers Part 5: https://surajincloud.com/understanding-http-server-in-go-handlefunc Why do we need this? While the DefaultServeMux In Golang does a great job of handling basic routing, there will come a point where you’ll want more control over your application’s routes. This is where the http.ServeMux steps in. It empowers you to explicitly define how your application responds to different routes, giving you fine-grained control over your server’s behaviour. ...

September 1, 2023 · 3 min · Suraj Narwade

Understanding HTTP Server in Go #5 - HandleFunc

Welcome back to the fifth instalment of our in-depth exploration of creating dynamic web servers using Golang! In this post, we’re diving into the fascinating world of HandlerFunc. As you’ve come to expect, GoLang provides us with an elegant and powerful way to handle requests through this mechanism. Let’s embark on this journey of discovery. Check out previous posts: Part 1: https://surajincloud.com/understanding-http-server-in-go-basic Part 2: https://surajincloud.com/understanding-http-server-in-go-using-httpserver-struct Part 3: https://surajincloud.com/understanding-http-server-in-go-handlers Part 4: https://surajincloud.com/understanding-http-server-in-go-multiple-handlers Let’s dive into the code to uncover the magic behind HandleFunc: ...

August 31, 2023 · 2 min · Suraj Narwade

Understanding HTTP Server in Go #4 - multiple handlers

In the previous blog post, we made our webserver useful for the first time where we printed Hello World instead of 404. Check out previous blog posts: Part 1: https://surajincloud.com/understanding-http-server-in-go-basic Part 2: https://surajincloud.com/understanding-http-server-in-go-using-httpserver-struct Part 3: https://surajincloud.com/understanding-http-server-in-go-handlers It was still useless as it was printing the same response on all the paths. Now in real real-world scenario, we won’t have just one URL right, We will need multiple paths and handlers and we still don’t have mux so now in this case we can still have multiple handlers ...

August 31, 2023 · 2 min · Suraj Narwade

Understanding HTTP Server in Go #3 - Handlers

Welcome to the exciting third instalment of our journey into creating web servers using Golang. In our previous articles, we covered the basics of creating a web server and explored the concept of custom servers using the http.Server struct. Check out the previous blog posts: \ Part 1:* https://surajincloud.com/understanding-http-server-in-go-basic \ part 2:* https://surajincloud.com/understanding-http-server-in-go-using-httpserver-struct However, as you might have noticed, our server was, until now, simply returning a 404 error. Fear not, because in this article, we’re about to breathe life into our server by diving into the realm of handlers. ...

August 30, 2023 · 2 min · Suraj Narwade

Understanding HTTP Server in Go #2 - using http.Server struct

Welcome back to the second part of our exploration into building web servers using GoLang! In the previous blog post, we discussed creating a basic web server using the http.ListenAndServe function. However, this does not give us more control over the server config and to gain more control and flexibility over the server’s behaviour, you’re in for a treat. In this article, we’ll delve into the world of custom servers by utilizing the http.Server struct. ...

August 29, 2023 · 2 min · Suraj Narwade

Understanding HTTP Server in Go #1 - Basic

The net/http package in Go offers a powerful set of tools for constructing HTTP servers to handle incoming requests and send responses. In this article, we’ll dive into setting up a basic HTTP server. Before we delve into the specifics, let’s familiarize ourselves with a couple of key terms: Multiplexer (or Mux): This is an essential component of an HTTP server that routes incoming requests to the appropriate handlers based on the requested paths or URLs. we will see this in detail in upcoming posts. With this foundation in mind, let’s explore how the net/http package simplifies the process of creating an HTTP server. ...

August 29, 2023 · 3 min · Suraj Narwade

Basic Authentication in HTTP API requests in Golang

In Golang, implementing basic authentication in an HTTP API request is relatively straightforward. Once we construct the request, then we have to call the SetBasicAuth() method and pass username & password package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "http://google.com", nil) if err != nil { log.Fatal(err) } req.SetBasicAuth("admin", "password") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", bodyText) } It is equivalent of, ...

February 12, 2023 · 2 min · Suraj Narwade

Difference between Setting & Adding the Headers in HTTP API in Golang

While working with APIs, headers are an essential aspect. Sometimes we set them, or sometimes we consume them and make decisions. When I was exploring headers in Golang, I came across two methods. Headers.Set() & Headers.Add() Initially, I thought they were the same. But then I wondered if they are the same, then why two methods? Let’s understand. I will be using a proxy which will use Add & Set on headers, as shown below, ...

January 18, 2023 · 2 min · Suraj Narwade

Why close the HTTP API response body in Golang? What if you don't...

In the previous article, we saw how to make the HTTP GET API request in Golang. While going through the example, we discussed closing the response body. In this article, Let’s discuss why it is essential to close the response body. What is the response body? The response body is a stream of data that is read from the server. Why should I close it? Ensure all data has been read and the resources associated with it are freed up. What happens if I don’t close it? It can lead to resource leaks. It can cause the program to consume more memory and resources than necessary, leading to performance issues. It can also cause issues with the underlying connection as transport may not reuse HTTP/1.x “keep-alive” TCP connections if the Body is not read to completion and closed. From the official docs, ...

January 14, 2023 · 2 min · Suraj Narwade

How to make an HTTP GET request in Golang?

Golang is one of the widely used languages for designing API Clients. While you design the client, one of the important aspects of the client is fetching the data from the API in some format using a GET request. Here’s the curl command example, which mimics the GET request which we will write code for in Golang, $ curl -XGET localhost:8080 {"message":"hello world !!!"} Note: I already have a basic webserver running, Hence I can get the output on localhost. ...

January 13, 2023 · 2 min · Suraj Narwade