Last active
March 13, 2019 12:22
-
-
Save stephenkeable-allies/4b72c4f57f039a759083 to your computer and use it in GitHub Desktop.
Global address lookup with Node JS using PostCoder Web from Allies Computing - https://www.alliescomputing.com/postcoder/address-lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
PostCoder Web Service V3 example | |
Allies Computing Ltd 2015 | |
This demo shows how to perform an address lookup in Node JS. | |
*/ | |
var https = require('https'); | |
// This is a test API Key which works for the postcode NR14 7PZ | |
// Get your API Key from www.alliescomputing.com/postcoder/sign-up | |
var api_key = 'PCW45-12345-12345-1234X'; | |
// String to use for an address search | |
var postcode = 'NR147PZ'; | |
// Note: The test API key above does not work with non UK country codes. | |
// Once you have signed up for a trial you can query any country | |
// using their ISO2 country code, such as US, FR, IE etc | |
// For a full list of country codes visit:- | |
// developers.alliescomputing.com/postcoder-web-api/address-lookup/international-addresses | |
var country_code = 'UK'; | |
// Set content type to JSON, XML is also available | |
var headers = { | |
'Content-Type': 'application/json' | |
}; | |
// Configure the request options | |
var options = { | |
host: 'ws.postcoder.com', | |
path: '/pcw/'+api_key+'/address/'+country_code+'/'+ encodeURIComponent(postcode), | |
method: 'GET', | |
headers: headers | |
}; | |
// Perform the request | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf-8'); | |
var responseString = ''; | |
res.on('data', function(data) { | |
responseString += data; | |
}); | |
res.on('end', function() { | |
//convert the response string to JSON object | |
var responseObject = JSON.parse(responseString); | |
// Output object to console | |
console.log(responseObject); | |
}); | |
}); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment