Back to Got

Axios

documentation/migration-guides/axios.md

15.1.04.2 KB
Original Source

> Back to homepage

Migration guides

You may think it's too hard to switch, but it's really not. 🦄

Axios

Axios is very similar to Got. The difference is that Axios targets browsers first, while Got fully makes use of Node.js features.

Common options

These options remain the same as well:

Renamed options

We deeply care about readability, so we renamed these options:

Changes in behavior

  • transformRequesthooks.beforeRequest
    • The API is different.
  • transformResponsehooks.afterResponse
    • The API is different.
  • baseUrlprefixUrl
    • The prefixUrl is prepended to relative string input. Absolute URLs bypass prefixUrl by default. Use allowAbsoluteUrls: false to reject absolute URLs when prefixUrl is set.
  • timeout
    • This option is now an object. You can now set timeouts on particular events!
  • responseType
    • Accepts 'text', 'json' or 'buffer'.

Breaking changes

Response

The response object is different as well:

The response.headers object remains the same.

Interceptors

Got offers hooks instead, which are more flexible.

Errors

Errors look the same, with the difference error.request returns a Got stream. Furthermore, Got provides more details to make debugging easier.

Abort

Got supports AbortController via the signal option.

Convenience methods

Convenience methods, such as axios.get(…) etc. remain the same: got.get(…). Instead of axios.create(…) use got.extend(…).

You're good to go!

Well, you have already come this far :tada:
Take a look at the documentation. It's worth the time to read it.
There are some great tips.

If something is unclear or doesn't work as it should, don't hesitate to open an issue.