A lightweight wrapper over Go’s HTTP client

Abhinav Bhardwaj
codealchemist
Published in
1 min readAug 27, 2021

While building an event-driven system which involved making HTTP requests to various external stakeholders, I felt the need of having an extremely lightweight wrapper over Go’s HTTP client.

This wrapper is supposed to provide a clean interface for setting various parameters in your request (query params, body, content type etc.) and handle various redundant tasks like the marshalling/unmarshalling of request/response, closing response body after it has been read etc.

Github repo: https://github.com/abhinav-codealchemist/custom-error-go

Usage:

import (
http "github.com/abhinav-codealchemist/http-wrapper-go/http_wrapper"
http2 "net/http"
)
req := requests.NewCancelOrderRequest()
endpoint := fmt.Sprintf("%s/%s", getHostUrl(), cancel_order_endpoint)
request := http.NewHttpRequestParams(endpoint, http2.MethodPost)
request.SetBody(req)
request.SetAuthToken(getAuthKey())
request.SetHost(getHostHeader())

apiResponse := responses.CreateOrderApiResponse{}
err = http.MakeApiCall(ctx, request, &apiResponse)
if err.Exists() {
return
}

Conclusion:

All you need is the request struct, endpoint, authentication params etc at your disposal and rest of the work can be taken care of with just a few lines of code thereby preventing code duplication and at the same easing the developer of redundant efforts involved in such cases.

If you have any feedback, reach out to me on Twitter, LinkedIn or Quora.

--

--

Abhinav Bhardwaj
codealchemist

Engineering @Uber | Ex-Zomato | DCEian | Software Developer | Open Source Enthusiast | Food Hogger | Student Forever