Class HTTPBuilder.RequestConfigDelegate
- Enclosing class:
HTTPBuilder
Encloses all properties and method calls used within the
HTTPBuilder.request(Object, Method, Object, Closure)
'config'
closure argument. That is, an instance of this class is set as the
closure's delegate. This allows the user to configure various parameters
within the scope of a single request.
All properties of this class are available from within the closure.
For example, you can manipulate various aspects of the
default request URI
for this request
by calling uri.path = '/api/location'
. This allows for the
ability to modify parameters per-request while leaving any values set
directly on the HTTPBuilder instance unchanged for subsequent requests.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object
private Object
private HttpContextDecorator
private org.apache.http.client.methods.HttpRequestBase
private Object
private URIBuilder
-
Constructor Summary
ConstructorsConstructorDescriptionRequestConfigDelegate
(Map<String, ?> args, org.apache.http.client.methods.HttpRequestBase request, groovy.lang.Closure successHandler) RequestConfigDelegate
(org.apache.http.client.methods.HttpRequestBase request, Object contentType, Map<?, ?> defaultRequestHeaders, Map<?, groovy.lang.Closure> defaultResponseHandlers) -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected groovy.lang.Closure
findResponseHandler
(int statusCode) Get the proper response handler for the response code.protected Object
Get the content-type of any data sent in the request body and the expected response content-type.Get theHttpContext
that will be used for this request.Map
<?, ?> Get request headers (including any default headers set on thisHTTPBuilder instance
).protected org.apache.http.client.methods.HttpRequestBase
Directly access the Apache HttpClient instance that will be used to execute this request.protected Object
The request content-type, if different from thecontentType
.Access the response handler map to set response parsing logic.getUri()
Use this object to manipulate parts of the request URI, like query params and request path.void
Convenience method to set a request content-type at the same time the request body is set.void
Set the request body.protected void
setContentType
(Object ct) Set the content-type used for any data in the request body, as well as theAccept
content-type that will be used for parsing the response.void
setContext
(org.apache.http.protocol.HttpContext ctx) Set theHttpContext
that will be used for this request.void
setHeaders
(Map<?, ?> newHeaders) Set request headers.protected void
setPropertiesFromMap
(Map<String, ?> args) Valid arguments: uriEither a URI, URL, or object whosetoString()
method produces a valid URI string.protected void
Assign a different content-type for the request than is expected for the response.void
Set the entire URI to be used for this request.
-
Field Details
-
request
private org.apache.http.client.methods.HttpRequestBase request -
contentType
-
requestContentType
-
responseHandlers
-
uri
-
headers
-
context
-
body
-
-
Constructor Details
-
RequestConfigDelegate
-
RequestConfigDelegate
public RequestConfigDelegate(Map<String, ?> args, org.apache.http.client.methods.HttpRequestBase request, groovy.lang.Closure successHandler) throws URISyntaxException- Throws:
URISyntaxException
-
-
Method Details
-
getUri
Use this object to manipulate parts of the request URI, like query params and request path. Example:builder.request(GET,XML) { uri.path = '../other/request.jsp' uri.query = [p1:1, p2:2] ... }
This method signature returns
Object
so that the complementarysetUri(Object)
method can accept various types.- Returns:
URIBuilder
to manipulate the request URI
-
setUri
Set the entire URI to be used for this request. Acceptable parameter types are:
URL
URI
URIBuilder
toString()
method produces a valid URI.Note that if you want to change just a portion of the request URI, (e.g. the host, port, path, etc.) you can call
getUri()
which will return aURIBuilder
which can manipulate portions of the request URI.- Parameters:
uri
- the URI to use for this request.- Throws:
URISyntaxException
- if an argument is given that is not a valid URI- See Also:
-
getRequest
protected org.apache.http.client.methods.HttpRequestBase getRequest()Directly access the Apache HttpClient instance that will be used to execute this request.- See Also:
-
getContentType
Get the content-type of any data sent in the request body and the expected response content-type. If the request content-type is expected to differ from the response content-type (i.e. a URL-encoded POST that should return an HTML page) then this value will be used for the response content-type, whilesetRequestContentType(String)
should be used for the request.- Returns:
- whatever value was assigned via
setContentType(Object)
or passed from theHTTPBuilder.defaultContentType
when this RequestConfigDelegate instance was constructed.
-
setContentType
Set the content-type used for any data in the request body, as well as theAccept
content-type that will be used for parsing the response. The value should be either aContentType
value or a String, i.e."text/plain"
. This will default toHTTPBuilder.getContentType()
for requests that do not explicitly pass acontentType
parameter (such asHTTPBuilder.request(Method, Object, Closure)
).- Parameters:
ct
- the value that will be used for theContent-Type
andAccept
request headers.
-
getRequestContentType
The request content-type, if different from thecontentType
.- Returns:
- either a
ContentType
value or String liketext/plain
-
setRequestContentType
Assign a different content-type for the request than is expected for the response. This is useful if i.e. you want to post URL-encoded form data but expect the response to be XML or HTML. The
getContentType()
will always control theAccept
header, and will be used for the request content unless this value is also explicitly set.Note that this method is used internally; calls within a request configuration closure should call
send(Object, Object)
to set the request body and content-type at the same time.- Parameters:
ct
- either aContentType
value or a valid content-type String.
-
setPropertiesFromMap
Valid arguments:- uri
- Either a URI, URL, or object whose
toString()
method produces a valid URI string. If this parameter is not supplied, the HTTPBuilder's default URI is used. - path
- Request path that is merged with the URI
- queryString
- an escaped query string
- query
- Map of URL query parameters
- headers
- Map of HTTP headers
- contentType
- Request content type and Accept header. If not supplied, the HTTPBuilder's default content-type is used.
- requestContentType
- content type for the request, if it is different from the expected response content-type
- body
- Request body that will be encoded based on the given contentType
queryString
andquery
are given,query
will be merged with (and potentially override) the parameters given as part ofqueryString
.- Parameters:
args
- named parameters to set properties on this delegate.- Throws:
URISyntaxException
- if the uri argument does not represent a valid URI
-
setHeaders
Set request headers. These values will be merged with anydefault request headers.
(The assumption is you'll probably want to add a bunch of headers to whatever defaults you've already set). If you only want to use values set here, simply callheaders.clear()
first. -
getHeaders
Get request headers (including any default headers set on this
HTTPBuilder instance
). Note that this will not include anyAccept
,Content-Type
, orContent-Encoding
headers that are automatically handled by any encoder or parsers in effect. Note that any values set here will override any of those automatically assigned values.Example:
headers.'Accept-Language' = 'en, en-gb;q=0.8'
- Returns:
- a map of HTTP headers that will be sent in the request.
-
send
Convenience method to set a request content-type at the same time the request body is set. This is a variation ofsetBody(Object)
that allows for a different content-type than what is expected for the response.Example:
http.request(POST,HTML) { /* request data is interpreted as a JsonBuilder closure by HTTPBuilder's default EncoderRegistry implementation * / send( 'text/javascript' ) { a : ['one','two','three'] } // response content-type is what was specified in the outer request() argument: response.success = { resp, html -> } }
Thesend
call is equivalent to the following:requestContentType = 'text/javascript' body = { a : ['one','two','three'] }
- Parameters:
contentType
- either aContentType
or equivalent content-type string like"text/xml"
requestBody
-
-
setBody
Set the request body. This value may be of any type supported by the associatedrequest encoder
. That is, the value ofbody
will be interpreted by the encoder associated with the currentrequest content-type
.- Parameters:
body
- data or closure interpreted as the request body- See Also:
-
encodeBody
public void encodeBody() -
findResponseHandler
protected groovy.lang.Closure findResponseHandler(int statusCode) Get the proper response handler for the response code. This is called by theHTTPBuilder
class in order to find the proper handler based on the response status code.- Parameters:
statusCode
- HTTP response status code- Returns:
- the response handler
-
getResponse
Access the response handler map to set response parsing logic. i.e.builder.request( GET, XML ) { response.success = { xml -> /* for XML content type, the default parser will return an XmlSlurper * / xml.root.children().each { println it } } }
- Returns:
-
getContext
Get theHttpContext
that will be used for this request. By default, a new context is created for each request.- Returns:
- See Also:
-
setContext
public void setContext(org.apache.http.protocol.HttpContext ctx) Set theHttpContext
that will be used for this request.- Parameters:
ctx
-
-