DEV Community

Paulund
Paulund

Posted on • Originally published at paulund.co.uk

4 1

How to debug axios requests

When working with APIs in your Javascript application it can be useful to be able to debug the requests and responses you're using to interact with the API.

Axios comes with a feature called interceptors that allow you to run some code before a request and after a response. This is a good place to add some code that runs on every request, you can put your debug messages in here.

axios.interceptors.request.use(request => {
    console.log('Starting Request', JSON.stringify(request, null, 2))
    return request
})

axios.interceptors.response.use(response => {
    console.log('Response:', JSON.stringify(response, null, 2))
    return response
})
Enter fullscreen mode Exit fullscreen mode

Now when you use axios to make requests to apis you'll get debug messages for each request and response.

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay