UK Postcode Lookup

  1. last year

    Paul B

    17 Jul 2016 Pre-Release Testers, Xojo Pro Europe (UK, Somerset)
    Edited last year by Norman P

    Is anyone in the UK doing postcode lookup using the Royal Mail website from XOJO.

    My postcode lookup needs are so small that I don't want to commit to an expensive API - i.e. I would be lucky to do 50 lookups a year, never mind a day so the royal mail site seems the way to go and T&C's allow for personal use. I would like to post to the site a postcode and recover the list of addresses it presents. Is this even possible in the background? The list could then be parsed to fill a few fields in my system (a little less crude than cut and paste).

    The website is here;

    http://www.postoffice.co.uk/postcode-finder

    An example postcode sn13ex produces 6 'possibles'.

    Can these be 'grabbed' and added to a popup menu from the returned 'popup menu'?

    I have done simple sock stuff with http post, but nothing like this so I don't know where to start :-(

    Web scraping can be unreliable and possibly violate certain terms of service.

    Here's a quick and dirty example using a non-Royal Mail API found at https://getaddress.io/ which lets you make 20 free API calls a day. I used a mix of old httpsocket and new dictionary because I find those easier to use for simple projects:

    dim apikey, postalcode, url, tmp1, tmp2(), addr as text
      dim ret as string
      dim https as new HTTPSecureSocket
      dim d as new xojo.Core.Dictionary
      dim a() as auto
      dim i, j as integer
      
      https.Secure = True
      https.ConnectionType = https.TLSv12
      
      apikey = "your-api-key"
      postalcode = "sn13ex"
      
      url = "https://api.getAddress.io/v2/uk/" + postalcode + "?api-key=" + apikey
      
      ret = https.get(url, 30).defineencoding(encodings.UTF8)
      
      d = xojo.data.ParseJSON(ret.ToText)
      
      a = d.value("Addresses")
      
      for i = 0 to a.Ubound
        tmp1 = a(i)
        tmp2 = tmp1.Split(",")
        
        for j = 0 to tmp2.Ubound
          
          addr = tmp2(j).trim
          
          if j = 0 then
            tmp1 = addr
          elseif addr <> "" then
            tmp1 = tmp1 + ", " +addr
          end if
        next
        
        PopupMenu1.AddRow(tmp1)
        
      next
  2. Scott G

    17 Jul 2016 Pre-Release Testers, Xojo Pro Answer

    Web scraping can be unreliable and possibly violate certain terms of service.

    Here's a quick and dirty example using a non-Royal Mail API found at https://getaddress.io/ which lets you make 20 free API calls a day. I used a mix of old httpsocket and new dictionary because I find those easier to use for simple projects:

    dim apikey, postalcode, url, tmp1, tmp2(), addr as text
      dim ret as string
      dim https as new HTTPSecureSocket
      dim d as new xojo.Core.Dictionary
      dim a() as auto
      dim i, j as integer
      
      https.Secure = True
      https.ConnectionType = https.TLSv12
      
      apikey = "your-api-key"
      postalcode = "sn13ex"
      
      url = "https://api.getAddress.io/v2/uk/" + postalcode + "?api-key=" + apikey
      
      ret = https.get(url, 30).defineencoding(encodings.UTF8)
      
      d = xojo.data.ParseJSON(ret.ToText)
      
      a = d.value("Addresses")
      
      for i = 0 to a.Ubound
        tmp1 = a(i)
        tmp2 = tmp1.Split(",")
        
        for j = 0 to tmp2.Ubound
          
          addr = tmp2(j).trim
          
          if j = 0 then
            tmp1 = addr
          elseif addr <> "" then
            tmp1 = tmp1 + ", " +addr
          end if
        next
        
        PopupMenu1.AddRow(tmp1)
        
      next
  3. David C

    17 Jul 2016 Pre-Release Testers, Xojo Pro Derby, ITM

    I have an SQLite database with all postcodes up until the number just after the space i.e. it gives the town, county and country, but not the street.

  4. Paul B

    17 Jul 2016 Pre-Release Testers, Xojo Pro Europe (UK, Somerset)

    Thanks both, https://getaddress.io/ offers good value for my needs an the example works flawlessly.

  5. 2 months ago

    Richard D

    Aug 11 Pre-Release Testers Europe (UK, London)

    thanks i got the code working for my application.. thanks guy... for the getaddress.io thingy

or Sign Up to reply!