Go Language Tip : Capturing HTTP Client Requests (incoming and outgoing)

Romin Irani
Romin Irani’s Blog
2 min readJan 31, 2017

I primarily use Golang for most of the Web APIs that I need to build out. Often, you want to check out the entire HTTP Request that has hit your API Endpoint, view some of the variables and so on.

Granted that one could use utilities like ngrok, you might want to look at one neat function called DumpRequest that is available in the httputil package it.

As per the documentation, the function has the following signature:

func DumpRequest(req *http.Request, body bool) ([]byte, error)

It will take in a request and provide you its HTTP 1.1 request format. It will dump out all the headers, parameters and even the body (if you provide a true value for the body parameter).

Here is a very rough snippet of what an outline of using this (note this is not production stuff):

Line #12 demonstrates use of the function. Sample browser responses for a GET and POST (some dummy header variables and BODY content) request are shown below:

HTTP GET Request
HTTP POST Request

Equally useful at times if to capture the HTTP Request that is sent out from your Go program. Let’s say that you are invoking an external API and wish to check your HTTP Request going out. You can use the DumpRequestOut function for that.

--

--

Romin Irani
Romin Irani’s Blog

My passion is to help developers succeed. ¯\_(ツ)_/¯