Tag Archives: nimlang

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"
}

Two Quick Nim Scripts

Today I Googled something like “julia lang compiled” for the umpteenth time in several months, and then I remembered that Nimrod existed, except it’s Nim these days, and it might make it to version 1.0 any week now. I decided to look around a bit, and I discovered that the documentation seems to have improved, and the language has gotten more fleshed out. I decided to try two small things I’ve been doing a lot of lately, SSH-ing and HTTP-getting and posting, and see what they look like in Nim.

Continue reading