0

Is it possible to get the client Ip address from the response of ta fetch? Something like this

  fetch(url)
    .then((response) => {
      console.log("My IP Address: ", response.originIP);
    })
yalpsideman
  • 23
  • 2
  • 6

2 Answers2

2

That is not possible. All the available methods of the fetch response are listed here: https://developer.mozilla.org/en-US/docs/Web/API/Response

You could however create a web service to return an IP address: How to get client's IP address using JavaScript?

Remi
  • 4,663
  • 11
  • 49
  • 84
0

Not directly, but you can use this to get your client's public IP address:

async function getClientIp() {
    const { ip } = await fetch('https://api.ipify.org?format=json', { method: 'GET' })
        .then(res => res.json())
        .catch(error => console.error(error));
    
    return ip || "0.0.0.0";
}

To know more about fetch: here.

To know more about ipify: here.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 10 '22 at 01:09