Contact Us
SaaS Log InXRay Login
Middleware

Adding middleware to Go HTTP client requests

Nastel Technologies®
October 11, 2022

The Go standard library has fantastic support for making HTTP requests. However, sometimes, a request needs to be modified or an action needs to be taken upon response (eg. logging a response).

 

In many cases, adding these logs to the every request would involve a lot of duplicate code. In other cases accessing the client might not be possible because of restricted access in a different package or third party library. Thus, I introduce RoundTrippers.

 

A RoundTripper can be added to an http client and executes a http transaction allowing the request to be inspected and modified and the response to be inspected and modified.

 

A RoundTripper can be set on a http client Transport.

 

The http.Client will resolve to the DefaultTransport if the client transport is nil. It’s fairly easy to chain RoundTrippers as middleware, the DefaultTransport round tripper should be considered as a base since it comes with handy defaults:

 

Now that the basics have been covered we can use a single chaining example which I originally saw by Philipp from GoWebExamples.com applied to web server middleware. It’s a simple little pattern that’s easy to understand.

 

From here writing custom middlewares for your needs is straightforward. Two examples below:

 

  • CustomTimer executes a function after the RoundTrip has finished and shows the time since. This demonstrates how operates can occur before the request is made, in this case we are capturing the start time and then executing code after the request has been made using a defer.
  • DumpResponse prints the response body to stdout. Similarly to CustomTimer, a defer is used to run the dump after the request has been made. The difference here is named response are used to access the response data.

 

Once you’ve written middleware that fits your needs, adding them to your HTTP client is as easy as creating an http.Client and setting the Transport.

 

This article originally appeared on forbes.com. To read the full article and see the images, click here.

Nastel Technologies is the global leader in Integration Infrastructure Management (i2M). It helps companies achieve flawless delivery of digital services powered by integration infrastructure by delivering tools for Middleware Management, Monitoring, Tracking, and Analytics to detect anomalies, accelerate decisions, and enable customers to constantly innovate, to answer business-centric questions, and provide actionable guidance for decision-makers. It is particularly focused on IBM MQ, Apache Kafka, Solace, TIBCO EMS, ACE/IIB and also supports RabbitMQ, ActiveMQ, Blockchain, IOT, DataPower, MFT, IBM Cloud Pak for Integration and many more.

 

The Nastel i2M Platform provides:

Comments

Write a comment
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment * This field is required!
First name * This field is required!
Email * Please, enter valid email address!

Schedule your Meeting

 


Schedule your Meeting


Subscribe

Schedule a Meeting to Learn More