Fullmenu null

 

13 February 2018

We are going to create a text file with all available operations for INE’s (statistical institute of Spain) from the endpoint http://servicios.ine.es//wstempus/js/ES/OPERACIONES_DISPONIBLES.

Create txt files in Groovy is very easy and same for making a request to any API REST with HttpBuilderNG, so we are going to use both.

HttpBuilderNG is a library that make us easier to access to any API and allows us to choose between 3 different implementations: core, apache or okhttp. For this script we are going to use core implementation.

@Grab(group='io.github.http-builder-ng', module='http-builder-ng-core', version='1.0.3') //(1)

import groovyx.net.http.*

String baseUrl = "http://servicios.ine.es"
String path = "/wstempus/js/ES/OPERACIONES_DISPONIBLES"

def httpBin = HttpBuilder.configure { //(2)
    request.uri = baseUrl
}

def operations = httpBin.get { //(3)
    request.uri.path = path
}

File ineOperations = new File('/tmp/ineOperations.txt') //(4)

operations.each {
    ineOperations << "${it}\n" //(5)
}
  1. Grab HttpBuilderNG library.

  2. Create configuration with base uri.

  3. Execute get operation.

  4. Get the text file we want to store all operations received.

  5. We create one line for each operation in the file


Script
@Grab(group='io.github.http-builder-ng', module='http-builder-ng-core', version='1.0.3') //(1)

import groovyx.net.http.*

String baseUrl = "http://servicios.ine.es"
String path = "/wstempus/js/ES/OPERACIONES_DISPONIBLES"

def httpBin = HttpBuilder.configure { //(2)
    request.uri = baseUrl
}

def operations = httpBin.get { //(3)
    request.uri.path = path
}

File ineOperations = new File('/tmp/ineOperations.txt') //(4)

operations.each {
    ineOperations << "${it}\n" //(5)
}