Hamza Sabljakovic
3 min readMar 10, 2019

Recently I wanted to automate a simple but in a way important task. Besides, it was supposed to be a temporary solution, if there is such a thing in software development as “temp fix”. The requirements were to invoke a couple of endpoints at a specific time, combine results and post data to a yet another endpoint. Due to the nature of the task and the fact that it didn’t really fit into any of existing services, lambda function in combination with cloud watch scheduled event (cron) sounded like a no-brainer.

Like with any side project of mine I like to put constraints on myself with the goal of making it more challenging and interesting. So, for this one, I decided to not include any external dependencies or use 3rd party tools such as serverless. Keeping it thin and as simple as possible.

Nevertheless, implementation of requirements was a very straightforward task, the only piece of the puzzle that I was missing from the top of my head was to how to do HTTP requests without installing any npm packages. Having that said, Node.js provides a native way of performing HTTP/s requests and it can be found inside of the http module. However, the built-in HTTP client comes with a bit of caveat. My problem with native HTTP module, hence, in my opinion, the popularity of countless other abstraction libraries around it, such as axios, request etc., is that API for using it is awkward. What I mean by awkward is that the HTTP module like many other node features is build using streams, resulting in HTTP code looking more or less like this:

Code example taken from this stackoverflow answer.

Streams are a great feature, don’t get me wrong, it’s just that I’m used to a higher level of abstraction when dealing with HTTP clients in other languages, a more straight forward approach when you give a payload and await for a result. On the side note, I have to admit that the get method looks decent but it’s not the focus of this post.

Although totally manageable and not a big deal for this particular task, having duplicated stream code for every request was hurting my eyes so I decided to look into ways to improve it. After spending some time with the HTTP module I ended up wrapping it inside of a promise, making it more readable and pleasant to work with by hiding the stream complexity inside of a reusable function.

Abstraction over the native http.request method

Utilizing the created post function looks as follows:

Consuming the post function.

In conclusion, it’s just a small portable function returning a promise that can be pasted inside of an AWS lambda for making HTTP requests without the need for installing any 3rd party dependencies. More importantly, it enables writing more expressive request code by abstracting away the stream API. Keep in mind that this implementation is not AWS lambda specific, lambda example is used to provide a more interesting case study.

Hamza Sabljakovic
Hamza Sabljakovic

Written by Hamza Sabljakovic

Software engineer based in Stockholm, Sweden.

Responses (4)