Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem using axios create method and how to use it

I’m trying to call public API to get IP address (https://api.ipify.org/) using axios (next app-typescript),

in my file index.ts :

import axios from "axios";
export const ipApi = axios.create({
    baseURL: "https://api.ipify.org",
})

and when i try to call it :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const getIP = await ipApi.get()

i got an error Expected 1-2 arguments, but got 0.ts(2554)
index.d.ts(223, 47): An argument for ‘url’ was not provided.

where did i go wrong ? is there something missing? Sorry for my bad english

>Solution :

Creating a axios instance with baseURL set just tell axios to prepend the URL given to the methods called using that instance. In your scenario you are calling the get() method without the required parameter url, and hence the error

The correct way of using the instance would be:

const getIP = await ipApi.get("/")

Which calls it on the root of the baseURL, essentially calling GET on https://api.ipify.org/

You can find the related docs here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading