REST Api to return the public IP

Hello, I need to create a REST API that returns the public IP of the user. This API will be consumed by an Angular.js front-end. I tried to create a simple api with the GetIP() method of HTTPRequestHandler, but it returns the server ip, not the user. Any tips?

Edit: I must not use third party URL.

Thanks.

Hi Ricardo,

I got the request ip using the OnRequest method on the API, using the GetIP() method of the HTTPRequestHandler


Kind Regards,

Leandro.

Leandro Correa wrote:

Hi Ricardo,

I got the request ip using the OnRequest method on the API, using the GetIP() method of the HTTPRequestHandler


Kind Regards,

Leandro.

Thanks for the response. This one works when I check the response on the browser or on the debbuger, but when I try to use this API on an OutSystems page, it returns the OutSystems server IP, for example 127.0.0.0.


Ricardo Matsuura wrote:

Leandro Correa wrote:

Hi Ricardo,

I got the request ip using the OnRequest method on the API, using the GetIP() method of the HTTPRequestHandler


Kind Regards,

Leandro.

Thanks for the response. This one works when I check the response on the browser or on the debbuger, but when I try to use this API on an OutSystems page, it returns the OutSystems server IP, for example 127.0.0.0.


This make sense, because the page is executing in the same server of the API, so will return the server IP.
I believe that I don’t really understand the propose, but maybe you could use the GetIP() function in the preparation of the page, instead of using an API. Or you are using the page to test only?  


Hi,

You need to run this as JavaScript in your browser. The next link describes two ways of doing this without using a 3th party API and also lists some 3th party APIs that can help, if you decide to ditch the requirement that you are not allowed to use a 3th part API

https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only

Regards,

Daniel

Hi Ricardo,

The OS Platform use this GetIP() function to detect the IP to block a malicious login attempt. 

Leandro Correa wrote:

Ricardo Matsuura wrote:

Leandro Correa wrote:

Hi Ricardo,

I got the request ip using the OnRequest method on the API, using the GetIP() method of the HTTPRequestHandler


Kind Regards,

Leandro.

Thanks for the response. This one works when I check the response on the browser or on the debbuger, but when I try to use this API on an OutSystems page, it returns the OutSystems server IP, for example 127.0.0.0.


This make sense, because the page is executing in the same server of the API, so will return the server IP.
I believe that I don’t really understand the propose, but maybe you could use the GetIP() function in the preparation of the page, instead of using an API. Or you are using the page to test only?  


I'm not using the OutSystems front-end, i'm using a angular front that consumes the OutSystems API. The Outsystems page is just for a quick test


Daniël Kuhlmann wrote:

Hi,

You need to run this as JavaScript in your browser. The next link describes two ways of doing this without using a 3th party API and also lists some 3th party APIs that can help, if you decide to ditch the requirement that you are not allowed to use a 3th part API

https://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only

Regards,

Daniel


This solution teach how to get the private IP with javascript, I need the public IP :(

Hi Ricardo,

I did a test with Postman and it was ok too. Maybe, it's something wrong with the javascript resquest... 

With postman it returns fine, but when i try to consume the API it returns this address.

Ricardo Matsuura wrote:

With postman it returns fine, but when i try to consume the API it returns this address.

Yes, because the GetIP() function is a server side and it's executing in the same server where you are exposing the API, so the request is being executed by localhost (127.0.0.1). 

So, this is not a wrong answer by the API.


Why not allowed to use external API?

https://www.ipify.org/ for example does an excellent job,, it is free open source.

Regards,

Daniel

Daniël Kuhlmann wrote:

Why not allowed to use external API?

https://www.ipify.org/ for example does an excellent job,, it is free open source.

Regards,

Daniel


because one of the customers mark this site as a malware. And I need to find a solution without using an external API

Rather than calling the ipify API you could also create an OutSystems Extension and use there C# libary

https://github.com/davidmus/Ipify.NET