pythonflaskherokuip-geolocation

Location changed to Ashburn after deploying to heroku


I am using openweathermap to get the weather data and detect the user's location and display the data...When I use it in localhost, I get the correct location, but when I deploy it to heroku, it shows the location as Ashburn...How to fix this?

I am using this for getting the user's location : https://ip-geolocation.whoisxmlapi.com/api/v1?apiKey=myApiKey

url1='https://ip-geolocation.whoisxmlapi.com/api/v1?apiKey=MyApiKey'
res = requests.request('GET', url1)
Jdata = res.json()
city = Jdata['location']['city']
completed_url = "http://api.openweathermap.org/data/2.5/weather? 
appid=myapiid&q="+city

r = requests.get(completed_url)
abcd = r.json()
abcdmain = abcd['main']

https://apiappanirudh.herokuapp.com/

Visit the above link for the app..

I want the app to detect the user's location and then display that...What is the reason behind the location changing and how to fix it?

Note: I am using flask.

Thanks.


Solution

  • When using an IP geolocation API, you should distinguish 2 scenarios: whether you call your geolocation API from the client-side and from the server-side.

    In the former case, it means you are querying the IP geolocation API from the client browser, using JavaScript for instance. In that case, most geolocation APIs provide an origin endpoint that you can invoke without specifying any IP address. You will get information for the IP address associated with the client running the browser. Here is an example using Ipregistry:

    https://api.ipregistry.co/?key=tryout&pretty=true

    In the latter case, you are receiving a request to your server and you are invoking the IP geolocation API from your server. It could be using PHP, NodeJS, Java, whatever programming language behind an application server or proxy. If we draw a diagram flow, this looks as follows:

    A client --> Your server --> IP geolocation API
    

    If you invoke, an IP geolocation API without specifying more information, the best that can be done by the geolocation API is to discover what is the IP address behind your server (i.e. from where the request originates). That's the issue you described and what you experienced.

    Hopefully, most IP geolocation APIs include an endpoint where you can specify what IP address to lookup data for. So when you receive a request from a client on your server, you need to extract the client IP address from your request and then use a dedicated IP geolocation API endpoint that allows passing an IP address.

    How to extract the client IP address from a request you receive on your server? This is specific to your application. That's why there is no one fit all solution. Most of the time there is a request header that includes the value you are looking for. For instance, the header X-Forwarded-For is quite common but not standard. In PHP you need to use $_SERVER['REMOTE_ADDR'].

    Let's say you identify your client IP address as being 200.77.121.230, then using Ipregistry, you would get location and threat data with an HTTP GET request to the next endpoint:

    https://api.ipregistry.co/200.77.121.230?key=tryout&pretty=true