Sunday, July 10, 2011

Groovy HTTP Builder- fetching JSON data over post and supply parameters

Right now I'm playing around with the MtGox api to issue online transactions to buy and sell bitcoins. Yes I'm still obsessed with these and don't think this is going to change anytime soon, since It's starting to actually generate money.

But the big pain right now is that you have togo to so many different websites todo a simple transaction and I really want to have this all combined in one single tool.

So the api exspects from you to be queried over post arguments and the first thing which came to my mind was to use the fantastic HttpBuilder in groovy.

so let's define a simple method to simplify life a bit for us

private executeQuery(Map parameter, String path) {

def http = new HTTPBuilder("")

def result = "" parameter, path: path, requestContentType: URLENC) { resp, json ->

if (resp.statusLine.statusCode == 200) {
result = json
else {
result = false


return result

and a second method to actually call this

def getCurrentBalance() {

if (MtGoxAccessHandler.isConfigured()) {
def values = executeQuery([

name: MtGoxAccessHandler.getUserName(),
pass: MtGoxAccessHandler.getPassword()],

return [usd: values.usds, coins: values.btcs]
else {
return false

The class MtGoxAccessHandler is just a little helper, which stores the username and password and allows me to simplify the code a bit and easy testing.

This post is also related to the google code project DeepBitView

No comments:

Post a Comment