Basic web service IP lookup


This page describes is a worked example that can be used on conjunction with the 08 REST Web Services training slides. 

Goal

This provides a set of instructions that a trainer (or trainee) can follow to create a basic web service using GeoIP lookup to get location data from a supplied IP address. With all steps from start to finish documented to create the process, this will give you a repeatable process to build and learn from.

Prerequisites

08 REST Web Services slides

You will need to log in to the Journey hub, and have a basic overview of the functionality on the home page.

This picks up from the example on page 13 of the slides.


Steps

First, you need to create a web service connection. Browse to the website http://freegeoip.net to confirm the end point we'll need.

Go to the admin tab, and connections. Add new connection called Free Geo IP.

Copy the http://freegeoip.net into the Endpoint, with a /json extension.  
 

Save and test this connection.

Back in the project editor, create the adaptor to use the connection.

Create new REST Web Service called Free Geo IP.

Add the Connection you just created and keep the Method as GET.

Into the request string, add a parameter, which will contain the IP address

%%ip%%

Save this. 

In the schema add a request child, and into this, add an ip element. 

Add this into the parameter slot, which we'll populate later.

You will also need to add an element for the return from the web service for the output geoip info.

Get the JSON output schema by going to the endpoint and testing this

http://freegeoip.net/json/92.27.153.126

Which gives an output of:

{"ip":"92.27.153.126","country_code":"GB","country_name":"United Kingdom","region_code":"ENG","region_name":"England","city":"Leicester","zip_code":"LE1","time_zone":"Europe/London","latitude":52.6333,"longitude":-1.1333,"metro_code":0}


In the schema, create a new child element called GeoIP

then 'Add a child record' to this, and copy in the output JSON. This should give you the format you need to store the geo location data.

Add the top level of this - geoIP - into the output slot of the web service adaptor.


Create a new GRAPH called 'IP Lookup' and add the IP lookup Free Geo IP rest service you have just created.

Drag out a goto branch and add a return node to this and set the Return Value to be the geoIP schema element just created.

Because we are connecting to an external service - bad things happen - so we need an error branch here too. Drag out a new node, set the link to be 'error' , replace the node with a return node, add a literal value of Error.


Next, go to the 'add listener to graph' headphones / cans. Change the listener type to be API, and set the OUTPUT to be the /request element of the schema.

COPY the Endpoint shown in the API Listener, then save this. Validate this.

Start this running. Open the Testing Console, clear any previous logs, and click run. Set this to duration = 10 and start testing. This will come up as 'running' and you will also see the headphones top left pulsing.

So, to test this, in your browser, paste the API string into the search box, and add ?ip= AN IP ADDRESS onto the end

https://api.xponent.com/api/v1/listener/59f65422ab91b435cc38be40f5becd4b?ip=8.8.8.8

This should give you a return! If not, check that the web service is still running.
Go to whatsmyipaddress.com to get your actual IP and try using your own IP. 8.8.8.8 is Googles IP.

You can add the proper output from the web service into the schema too so you can capture all of this. From the texting console, Display JSON, and then get the request element. If you already have a request child, you can just add the IP bits. Or add the whole lot from request.

{
	"ip": "8.8.8.8",
	"_kw": {
		"headers": {
			"accept-language": "en-US,en;q=0.8",
			"accept-encoding": "gzip, deflate, br",
			"x-forwarded-port": "443",
			"x-forwarded-for": "92.27.153.126, 172.31.0.77",
			"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
			"upgrade-insecure-requests": "1",
			"host": "api.xponent.com",
			"x-forwarded-proto": "https",
			"cookie": "_ga=GA1.2.373325656.1499784849; __hstc=46294904.5133c1c72f649b3feba69760da92a74b.1499784850469.1501078733571.1501155352694.7; hubspotutk=5133c1c72f649b3feba69760da92a74b",
			"cache-control": "max-age=0",
			"x-real-ip": "172.31.0.77",
			"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
		}
	}
}




{
	"request": {
		"ip": "8.8.8.8",
		"_kw": {
			"headers": {
				"accept-language": "en-US,en;q=0.8",
				"accept-encoding": "gzip, deflate, br",
				"x-forwarded-port": "443",
				"x-forwarded-for": "92.27.153.126, 172.31.0.77",
				"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
				"upgrade-insecure-requests": "1",
				"host": "api.xponent.com",
				"x-forwarded-proto": "https",
				"cookie": "_ga=GA1.2.373325656.1499784849; __hstc=46294904.5133c1c72f649b3feba69760da92a74b.1499784850469.1501078733571.1501155352694.7; hubspotutk=5133c1c72f649b3feba69760da92a74b",
				"cache-control": "max-age=0",
				"x-real-ip": "172.31.0.77",
				"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"
			}
		}
	}
}



To add some extra validation into this, you can use some javascript to validate IP addresses before they get sent. So create a new Javascript node, called validateIP. Add an argument called 'ip' 

then paste this in to create a regex for IP validation

if (/^\d+\.\d+\.\d+\.\d+$/.test(ip)) { 
	return ip; 
}
return '127.0.0.1';


This gives a default for anything that is not a valid IP address.

Add this to the graph, replace the start node marker to this, then put this ahead of the IP Lookup. You need to add the ip and return values into /request/ip into the ip slot, as this is where the ip address comes in and the return value as /geoip/ip.


Privacy Policy
© 2022 CSG International, Inc.