(Quick Reference)

3 Making Requests - Reference Documentation

Authors: Bud Byrd

Version: 2.0.2

3 Making Requests

To use the Jersey Request Builder, simply inject the jerseyRequestBuilder bean into any other bean managed by Spring. Examples of managed Spring beans include Grails controllers and services. An example follows.

package com.budjb

import com.budjb.requestbuilder.JerseyRequestBuilder

class ExampleController { JerseyRequestBuilder jerseyRequestBuilder

def index = {

} }

To use the Jersey Request Builder in a POGO (plain old groovy object), it can be retrieved from the applicationContext using the Holders class as follows.

import grails.util.Holders

class Example { JerseyRequestBuilder jerseyRequestBuilder = Holders.applicationContext.getBean("jerseyRequestBuilder") }

The Jersey Request Builder can be configured and used in several ways, depending on how the author intends to use it.

3.1 HTTP Methods

The HTTP specification defines several request methods. Each method has a different intended use, and there are some behavior requirements around them.

The Jersey Request Builder supports many of these methods, as outlined below:

MethodJersey Request Builder Method
GETget()
POSTpost()
PUTput()
DELETEdelete()
HEADhead()
OPTIONSoptions()
TRACEtrace()

3.2 Simple Usage

The simplest way to use the Jersey Request Builder is to use the convenience methods that only require a URL, and optionally the body on methods that support it. They can be used as follows:
// GET example
jerseyRequestBuilder.get("http://www.example.com")

// POST example jerseyRequestBuilder.post("http://www.example.com", "payload")

// PUT example jerseyRequestBuilder.put("http://www.example.com", "payload")

// DELETE example jerseyRequestBuilder.delete("http://www.example.com")

3.3 DSL Usage

Using the DSL-style functions is the most common use of the Jersey Request Builder. These methods provide an easy to use, centralized way to build a request with varying parameters. This documentation will not go into detail about all of the options available with the Jersey Request Builder, but all of these options are documented completely in the reference section.

All of the HTTP methods can use the DSL-based methods.

jerseyRequestBuilder.post {
    uri = "http://www.example.com"
    body = ["foo": "bar"]
    headers = [
        "X-Auth-Key": "1321d980-3ceb-11e3-b872-081196ac8a64"
    ]
}

3.4 Request Properties

The Jersey Request Builder also allows the author to build a request gradually through a properties object. All of the properties that can be set inside of the DSL-based methods can also be set directly on the properties itself.

RequestProperties requestProperties = new RequestProperties()

requestProperties.uri = "http://www.example.com" requestProperties.accept = "text/html"

def result = jerseyRequestBuilder.get(requestProperties)