Package com.avoka.component.http

Provides a Fluent HTTP client library using Apache HTTP Components.

The example below performs REST request and returns a JSON object.

import com.avoka.component.http.GetRequest

def params = [:]
def params.id = request.getParameter('customerId')
def params.account = request.getParameter('accountId')

def username = serviceParameters.username
def password = serviceParameters.password

// execute GET request and return a HttpResponse object 
def response = new GetRequest('https://service.mycorp.com/secure/rest/accounts/')
      .setParams(params)
      .setBasicAuth(username, password)
      .execute()

if (response.status == 200) {
    // get JSON object from the response
    def json = response.getJsonContent()
    ...
     
} else if (response.status == 404) {
    // object not found
    ...
     
} else {
    throw new RuntimeException(response.statusLine)
} 

The example below performs POST request and converts the response into a JSON document.

import com.avoka.component.http.PostRequest

def message = '{ "name":"John Doe", "email":"[email protected]", "age":42 }'

def username = serviceParameters.username
def password = serviceParameters.password

// execute POST request and return a HttpResponse object
def response = new PostRequest('https://service.mycorp.com/secure/rest/accounts/')
      .setMessage(message)
      .setBasicAuth(username, password)
      .execute()

// check HttpResponse status code
if (response.status != 200) {
    throw new RuntimeException(response.statusLine)
}

// get JSON object from the response
def json = response.getJsonContent() 
...