Axios response time — Capture and log

Hamza Sabljakovic
2 min readMar 27, 2020

--

Photo by Icons8 Team on Unsplash

Notice: Make sure to use axios version different then 0.19.0 as it introduced this feature/bug.

Github gist will full source code here.

In this post, we will explore how to capture and/or log HTTP response time with axios. We will be expending on one of my previous posts about Axios interceptors on how to log request and response (body and headers).

Without further ado, the first step is to record time when an HTTP request started, this can be easily done with a request interceptor.

Request interceptor — Record when a request is started

Next, when we receive a response from a server, in the response interceptor, we need to add the following logic.

Response interceptor — Logs response time (only for successful requests)

The above interceptor will log only successful requests, to add support for failed requests, we need to register an additional handler within the same response interceptor.

Response interceptor — Support for 4XX & 5XX responses

If for some reason, the consumer needs to access the response time value, let’s say showing it in the UI, that can be easily achieved with a slight modification of the response interceptor.

Response interceptor — Attach response time to be consumed

As you can see, it’s pretty straight forward to add response time logging to axios, this might get useful performance debugging tool if other solutions such APM are not available.

The above-shown way of measuring execution time is not the most precise you can get with NodeJS. If you need a greater precision you should checkout this post.

Full source code here.

--

--

Hamza Sabljakovic
Hamza Sabljakovic

Written by Hamza Sabljakovic

Software engineer based in Stockholm, Sweden.

Responses (3)