Wednesday, October 2, 2013

Australia Post API and CFML

Someone on Facebook mentioned needing to work with Australia Posts API for getting prices calculated, so I figured I'd have a go!
It took me about a half hour but my simple test works fine.
First you need to sign up for an account and an API key, you can't get the API key without an account, and you need your API key when talking to their API server.
The test code is very very simple:

Here we're calculating the cost for a domestic parcel, going from postcode 2128 to postcode 6000, the package has a length of 96cm, width of 8cm, height of 5cm and a weight of 1.5 kilos.
You will receive an XML response with lots of nice info such as the price, just do a cfdump if you want to take a look at the return.

You can find the full API doc here: http://auspost.com.au/devcentre/assets/pdfs/pac-pcs-technical-specification.pdf
Hopefully someone will have use for this simple example!

--Edit with update--

I've ran the following code on CF 9.0.2 and it runs just fine:

12 comments:

  1. Hey i've tried your code and it's giving me a "Content is not allowed in prolog." error any ideas why?

    ReplyDelete
  2. Hi Sebastien, I just tried the code on OpenBD 2.0.1 and it's working fine for me, what engine and version are you using?

    Did you put your API key in the cfhttpparam?

    I haven't seen that error before, but google suggests it has something to do with the underlying JAVA code not liking the XML, are you forcing a certain encoding?

    ReplyDelete
  3. Hey Marcus, strange i've tried the same code locally on CF10 server and seems to work, but when i try it on the live CF9 server it doesn't odd,

    Anyway i've parsed the XML successfully locally but having issues when trying to extract some of the XML data e.g. (xmlSearch(MyXML, "services/service")) keeps saying "Complex object types cannot be converted to simple values" any pointers? i've used this code before to extract XML data but never had these issues

    Thanks for your help

    ReplyDelete
    Replies
    1. Hi Freelance,

      Yay for the differences between CF versions.

      On CF9 if I remember you have to be more explicit, services contains the whole shebang, so if you want to pull let's say the price, try changing your service/services to services/service/price (It should work, but I don't have a CF9 server handy to test it out on)

      I'm trying to grab a CF9 dev download to test it out with, I'll post an update here when I test it (Feel free to try it and post too)

      Delete
    2. As a sidenote, on OpenBD I can just do:
      #XmlParse(xmlsource).services.service.price#

      That just pulls the price and outputs as text, I don't think that'll work on CF9 but I'll test that too once I found a download.

      Delete
    3. Sebastien MoralesJune 5, 2014 at 4:34 PM

      Just figured it out i ended up using this code

      ----------------------------------------------------------------







      #xmlDoc#


      That seems to work on my local CF10 server but i have yet to try it on CF9 fingers crossed!

      Delete
  4. Sebastien MoralesJune 5, 2014 at 5:11 PM

    And in classic Coldfusion style, the code just spits out "Content is not allowed in prolog." on CF9

    ReplyDelete
  5. Sebastien, I've updated the post with my CF9 code test.

    ReplyDelete
  6. Sebastien MoralesJune 5, 2014 at 9:54 PM

    Hey Marcus i finally figured it out the CFHTTP request had to have charset="UTF-8" parameter!

    ReplyDelete
    Replies
    1. Sebastien, that's great :)

      My server (And dev box) use UTF-8 as default so I didn't run into that, glad you figured it out!

      Delete
  7. Thanks for your help! Greatly appreciated!

    ReplyDelete
    Replies
    1. No problem, but you figured it out yourself :)

      Delete