Make a GET Request in Nimrod

In this code snippet we make a GET request to Google’s Maps API. We’ll use the strutils module to handle some of the strings.

I couldn’t get anything to work until I updated my Nimrod installation to 0.11.2 or something. I had 0.10.? before and I had zero luck with this. I think I read there was a bug in httpclient. So if you’re having trouble, make sure you’ve installed the latest (stable) compiler.

import httpclient
import strutils

proc mapreq( lat, lon: string ) =
    var coord = strutils.join( [ lat, lon ], "," )
    coord = strutils.join( [ coord, "sensor=false" ], "&" )
    var address = "http://maps.google.com/maps/api/elevation/json?locations="
    var call = strutils.join( [ address, coord ], "" )
    var res = httpclient.get( call )
    echo( res.body )

mapreq( "32.0", "-102.0" )

Then compile and run this as,

nim c -r coord.nim

And you should see this in your terminal,

{
   "results" : [
      {
         "elevation" : 836.9058837890625,
         "location" : {
            "lat" : 32,
            "lng" : -102
         },
         "resolution" : 9.543951988220215
      }
   ],
   "status" : "OK"
}