HTTP methods and parameters
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.
HTTP methods
Methods may be specified with correspondingly named request-building methods.
def myPost = myRequest.POST
Other HTTP methods can be specified in the same way.
HEADGETPOSTPUTDELETEPATCHTRACEOPTIONS
POST parameters
To add form-encoded parameters to the request body, you can use
RequestBuilder#addParameter
method.
def myPostWithParams = myPost.addParameter("key", "value")
POST verb
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
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")
You can also add query parameters with the <<?
verb.
def myRequestWithParams = myRequest <<? Map("key" -> "value")
PUT a file
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.