Having defined a request endpoint using either url
, or host
and
the path-appending verb, you may now wish to change the HTTP method
from its default of GET.
Methods may be specified with correspondingly named request-building methods.
def myPost = myRequest.POST
Other HTTP methods can be specified in the same way.
HEAD
GET
POST
PUT
DELETE
PATCH
TRACE
OPTIONS
To add form-encoded parameters to the request body, you can use
RequestBuilder#addParameter
method.
def myPostWithParams = myPost.addParameter("key", "value")
The <<
verb sets the request method to POST and adds form-encoded
parameters to the body at once:
def myPostWithParams = myRequest << Map("key" -> "value")
You can also POST an arbitrary string. Be sure to set MIME media type and character encoding:
def myRequestAsJson = myRequest.setContentType("application/json", "UTF-8")
def myPostWithBody = myRequestAsJson << """{"key": "value"}"""
Query parameters can be appended to request paths regardless of the method. These should be added after all path elements.
def myRequestWithParams = myRequest.addQueryParameter("key", "value")
Query parameter names can repeat in case you need provide multiple values for a query parameter key.
def myRequestWithParams = myRequest
.addQueryParameter("key", "value1")
.addQueryParameter("key", "value2")
Query parameters can also be added without values to create urls such as
http://mydomain.com?param
by just providing the key:
def myRequestWithParams = myRequest
.addQueryParameter("key")
You can also add query parameters with the <<?
verb.
def myRequestWithParams = myRequest <<? Map("key" -> "value")
The <<?
verb can consume any kind of Iterable
that contains a
(String, String)
, so if you’d like to use the verb form to add multiple
query parameters with the same key, you’d just switch to using a List
:
def myRequestWithParams = myRequest <<? List(
("key", "value1"),
("key", "value2")
)
Similar to the POST verb, Dispatch supplies a <<<
verb to apply the
PUT method and set a java.io.File
as the request body.
def myPut = myRequest <<< myFile
If you wish to supply a string instead of a file, use a setBody
method of the RequestBuilder class. Its variants support a
number of input types and do not imply a particular HTTP method.