# Class RequestBuilder

java.lang.Object
com.codename1.io.rest.RequestBuilder

public class RequestBuilder extends Object
This class is used to build, invoke the http request and to get the http response

- ## Method Summary

  Modifier and Type
  Method
  Description
  `RequestBuilder`
  `acceptJson()`
  Add accept json header to the request
  `RequestBuilder`
  `basicAuth(String username, String password)`
  Add a basic authentication Authorization header
  `RequestBuilder`
  `bearer(String token)`
  Add an authorization bearer header, this is shorthand for `header("Authorization", "Bearer " + token)`
  `RequestBuilder`
  `body(Data body)`
  Sets the request body lazily.
  `RequestBuilder`
  `body(PropertyBusinessObject body)`
  Sets the request body to the JSON matching the given object
  `RequestBuilder`
  `body(String bodyContent)`
  Sets the request body
  `RequestBuilder`
  `cacheMode(ConnectionRequest.CachingMode cache)`
  Sets the caching mode for this request, see `com.codename1.io.ConnectionRequest#getCacheMode()`
  `RequestBuilder`
  `contentType(String s)`
  Sets the value of the content type
  `RequestBuilder`
  `cookiesEnabled(boolean cookiesEnabled)`
  Sets the cookiesEnabled parameter.
  `ConnectionRequest`
  `fetchAsBytes(OnComplete<Response<byte[]>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `ConnectionRequest`
  `fetchAsJsonMap(OnComplete<Response<Map>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `ConnectionRequest`
  `fetchAsProperties(OnComplete<Response<PropertyBusinessObject>> callback, Class type)`
  Executes the request asynchronously and writes the response to the provided Callback.
  `ConnectionRequest`
  `fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type)`
  Executes the request asynchronously and writes the response to the provided Callback.
  `ConnectionRequest`
  `fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type, String root)`
  Executes the request asynchronously and writes the response to the provided Callback.
  `ConnectionRequest`
  `fetchAsString(OnComplete<Response<String>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `Response<byte[]>`
  `getAsBytes()`
  Executes the request synchronously
  `void`
  `getAsBytesAsync(Callback<Response<byte[]>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `Response<Map>`
  `getAsJsonMap()`
  Executes the request synchronously
  `ConnectionRequest`
  `getAsJsonMap(SuccessCallback<Response<Map>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `ConnectionRequest`
  `getAsJsonMap(SuccessCallback<Response<Map>> callback, FailureCallback<? extends Object> onError)`
  Executes the request asynchronously and writes the response to the provided Callback
  `void`
  `getAsJsonMapAsync(Callback<Response<Map>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `Response<PropertyBusinessObject>`
  `getAsProperties(Class type)`
  Executes the request synchronously
  `Response<List<PropertyBusinessObject>>`
  `getAsPropertyList(Class type)`
  Executes the request synchronously
  `Response<List<PropertyBusinessObject>>`
  `getAsPropertyList(Class type, String root)`
  Executes the request synchronously
  `Response<String>`
  `getAsString()`
  Executes the request synchronously
  `void`
  `getAsStringAsync(Callback<Response<String>> callback)`
  Executes the request asynchronously and writes the response to the provided Callback
  `String`
  `getRequestUrl()`
  `RequestBuilder`
  `gzip()`
  Sets the request to be a gzip request
  `RequestBuilder`
  `header(String key, String value)`
  Add a header to the request
  `RequestBuilder`
  `insecure(boolean insecure)`
  Turns off checking to make sure that SSL certificate is valid.
  `RequestBuilder`
  `jsonContent()`
  Sets both the content type and accept headers to "application/json"
  `RequestBuilder`
  `onError(ActionListener<NetworkEvent> error)`
  Invoked for exceptions or failures such as disconnect.
  `RequestBuilder`
  `onError(ActionListener<NetworkEvent> error, boolean replace)`
  Invoked for exceptions or failures such as disconnect
  `RequestBuilder`
  `onErrorCode(ErrorCodeHandler<PropertyBusinessObject> err, Class errorClass)`
  In case of an error this method is invoked asynchronously to process the error content with the JSON data and places it into a business object in the callback
  `RequestBuilder`
  `onErrorCodeBytes(ErrorCodeHandler<byte[]> err)`
  In case of an error this method is invoked asynchronously to process the error content with the byte array data
  `RequestBuilder`
  `onErrorCodeJSON(ErrorCodeHandler<Map> err)`
  In case of an error this method is invoked asynchronously to process the error content with the JSON data
  `RequestBuilder`
  `onErrorCodeString(ErrorCodeHandler<String> err)`
  In case of an error this method is invoked asynchronously to process the error content with the JSON data
  `RequestBuilder`
  `pathParam(String key, String value)`
  Add a path param to the request.
  `RequestBuilder`
  `postParameters(Boolean postParameters)`
  Overrides the default behavior of methods so they can be sent using the post/get method
  `RequestBuilder`
  `priority(byte priority)`
  Sets the priority of the request.
  `RequestBuilder`
  `queryParam(String key, String value)`
  Add a query parameter to the request
  `RequestBuilder`
  `queryParam(String key, String[] values)`
  Add multiple query parameter values to the request using same key.
  `RequestBuilder`
  `readTimeout(int timeout)`
  Sets the request read timeout.
  `RequestBuilder`
  `timeout(int timeout)`
  Sets the request timeout
  `RequestBuilder`
  `useBoolean(boolean useBoolean)`
  Indicates if JSON should treat boolean values as Boolean.
  `RequestBuilder`
  `useLongs(boolean useLongs)`
  Indicates if JSON should treat non-decimal numeric values as Long.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### insecure

    public RequestBuilder insecure(boolean insecure)

    Turns off checking to make sure that SSL certificate is valid.

    ###### Parameters

    - `insecure`: true to disable ssl certificate checking

    ###### Returns

    this request builder

  - ### useBoolean

    public RequestBuilder useBoolean(boolean useBoolean)

    Indicates if JSON should treat boolean values as Boolean. This values is set implicitly to true when reading property business objects

    ###### Parameters

    - `useBoolean`: true to return Boolean objects in JSON Maps

    ###### Returns

    this request builder

  - ### useLongs

    public RequestBuilder useLongs(boolean useLongs)

    Indicates if JSON should treat non-decimal numeric values as Long. If not set, uses the current default from the static value in JSONParser.isUseLongs

    ###### Parameters

    - `useLongs`: true to return Long objects in JSON Maps

    ###### Returns

    this request builder

  - ### cacheMode

    public RequestBuilder cacheMode(ConnectionRequest.CachingMode cache)

    Sets the caching mode for this request, see `com.codename1.io.ConnectionRequest#getCacheMode()`

    ###### Parameters

    - `cache`: the cache mode

    ###### Returns

    RequestBuilder instance

  - ### postParameters

    public RequestBuilder postParameters(Boolean postParameters)

    Overrides the default behavior of methods so they can be sent using the post/get method

    ###### Parameters

    - `postParameters`: true to force post, false to use get method. Defaults to true for all methods other than GET

    ###### Returns

    RequestBuilder instance

  - ### contentType

    public RequestBuilder contentType(String s)

    Sets the value of the content type

    ###### Parameters

    - `s`: the content type

    ###### Returns

    RequestBuilder instance

  - ### priority

    public RequestBuilder priority(byte priority)

    Sets the priority of the request.

    ###### Parameters

    - `priority`: The priority.

    ###### Returns

    RequestBuilder instance.

    ###### Since

    8.0

    ###### See also

    - ConnectionRequest#setPriority(byte)

  - ### cookiesEnabled

    public RequestBuilder cookiesEnabled(boolean cookiesEnabled)

    Sets the cookiesEnabled parameter.

    ###### Parameters

    - `cookiesEnabled`: True to enable cookies. False to disable.

    ###### Returns

    RequestBuilder instance.

    ###### Since

    8.0

  - ### pathParam

    public RequestBuilder pathParam(String key, String value)

    Add a path param to the request. For example if the request url is: http://domain.com/users/{id} The path param can be - key="id", value="1" When the request executes the path would be: http://domain.com/users/1

    ###### Parameters

    - `key`: the identifier key in the request.

    - `value`: the value to replace in the url

    ###### Returns

    RequestBuilder instance

  - ### queryParam

    public RequestBuilder queryParam(String key, String value)

    Add a query parameter to the request

    ###### Parameters

    - `key`: param key

    - `value`: param value

    ###### Returns

    RequestBuilder instance

  - ### queryParam

    public RequestBuilder queryParam(String key, String[] values)

    Add multiple query parameter values to the request using same key.

    ###### Parameters

    - `key`: param key

    - `values`: param values

    ###### Returns

    RequestBuilder instance

    ###### Since

    8.0

  - ### header

    public RequestBuilder header(String key, String value)

    Add a header to the request

    ###### Parameters

    - `key`

    - `value`

    ###### Returns

    RequestBuilder instance

  - ### body

    public RequestBuilder body(String bodyContent)

    Sets the request body

    ###### Parameters

    - `bodyContent`: request body content

    ###### Returns

    RequestBuilder instance

  - ### body

    public RequestBuilder body(Data body)

    Sets the request body lazily.

    ###### Parameters

    - `body`: Wrapper for the request body that knows how to append to an output stream.

    ###### Returns

    RequestBuilder instances

    ###### Since

    7.0

    ###### See also

    - #body(java.lang.String)

  - ### body

    public RequestBuilder body(PropertyBusinessObject body)

    Sets the request body to the JSON matching the given object

    ###### Parameters

    - `body`: request body

    ###### Returns

    RequestBuilder instance

  - ### onErrorCodeBytes

    public RequestBuilder onErrorCodeBytes(ErrorCodeHandler<byte[]> err)

    In case of an error this method is invoked asynchronously to process the error content with the byte array data

    ###### Parameters

    - `err`: the content of the error response

    ###### Returns

    RequestBuilder instance

  - ### onErrorCodeJSON

    public RequestBuilder onErrorCodeJSON(ErrorCodeHandler<Map> err)

    In case of an error this method is invoked asynchronously to process the error content with the JSON data

    ###### Parameters

    - `err`: the content of the error response

    ###### Returns

    RequestBuilder instance

  - ### onErrorCode

    public RequestBuilder onErrorCode(ErrorCodeHandler<PropertyBusinessObject> err, Class errorClass)

    In case of an error this method is invoked asynchronously to process the error content with the JSON data and places it into a business object in the callback

    ###### Parameters

    - `err`: the content of the error response

    - `errorClass`: the class of the business object into which the data is parsed

    ###### Returns

    RequestBuilder instance

  - ### onErrorCodeString

    public RequestBuilder onErrorCodeString(ErrorCodeHandler<String> err)

    In case of an error this method is invoked asynchronously to process the error content with the JSON data

    ###### Parameters

    - `err`: the content of the error response

    ###### Returns

    RequestBuilder instance

  - ### onError

    public RequestBuilder onError(ActionListener<NetworkEvent> error)

    Invoked for exceptions or failures such as disconnect. Replaces any existing callbacks previously registered with `#onError(com.codename1.ui.events.ActionListener)`

    ###### Parameters

    - `error`: callback for a networking error

    ###### Returns

    RequestBuilder instance

    ###### See also

    - #onError(com.codename1.ui.events.ActionListener, boolean)

  - ### onError

    public RequestBuilder onError(ActionListener<NetworkEvent> error, boolean replace)

    Invoked for exceptions or failures such as disconnect

    ###### Parameters

    - `error`: callback for a networking error

    - `replace`: @param replace If true, replaces the existing errorCallback(s) with the handler provided.

    ###### Returns

    RequestBuilder instance

    ###### Since

    7.0

  - ### timeout

    public RequestBuilder timeout(int timeout)

    Sets the request timeout

    ###### Parameters

    - `timeout`: request timeout in milliseconds

    ###### Returns

    RequestBuilder instance

  - ### readTimeout

    public RequestBuilder readTimeout(int timeout)

    Sets the request read timeout. Only used if `ConnectionRequest#isReadTimeoutSupported()` is true on this platform.

    ###### Parameters

    - `timeout`: The timeout.

    ###### Returns

    RequestBuilder instance.

  - ### gzip

    public RequestBuilder gzip()

    Sets the request to be a gzip request

    ###### Returns

    RequestBuilder instance

  - ### acceptJson

    public RequestBuilder acceptJson()

    Add accept json header to the request

    ###### Returns

    RequestBuilder instance

  - ### jsonContent

    public RequestBuilder jsonContent()

    Sets both the content type and accept headers to "application/json"

    ###### Returns

    RequestBuilder instance

  - ### basicAuth

    public RequestBuilder basicAuth(String username, String password)

    Add a basic authentication Authorization header

    ###### Returns

    RequestBuilder instance

  - ### bearer

    public RequestBuilder bearer(String token)

    Add an authorization bearer header, this is shorthand for `header("Authorization", "Bearer " + token)`

    ###### Parameters

    - `token`: the authorization token

    ###### Returns

    RequestBuilder instance

  - ### fetchAsString

    public ConnectionRequest fetchAsString(OnComplete<Response<String>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: invoked with the result of the builder query

    ###### Returns

    the ConnectionRequest instance

  - ### getAsStringAsync

    public void getAsStringAsync(Callback<Response<String>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Deprecated

    please use `#fetchAsString(com.codename1.util.OnComplete)` instead

  - ### getAsString

    public Response<String> getAsString()

    Executes the request synchronously

    ###### Returns

    Response Object

  - ### fetchAsBytes

    public ConnectionRequest fetchAsBytes(OnComplete<Response<byte[]>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Returns

    the connection request instance

  - ### getAsBytesAsync

    public void getAsBytesAsync(Callback<Response<byte[]>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Deprecated

    use `#fetchAsBytes(com.codename1.util.OnComplete)` instead

  - ### getAsBytes

    public Response<byte[]> getAsBytes()

    Executes the request synchronously

    ###### Returns

    Response Object

  - ### fetchAsJsonMap

    public ConnectionRequest fetchAsJsonMap(OnComplete<Response<Map>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

  - ### fetchAsProperties

    public ConnectionRequest fetchAsProperties(OnComplete<Response<PropertyBusinessObject>> callback, Class type)

    Executes the request asynchronously and writes the response to the provided Callback. This fetches JSON data and parses it into a properties business object

    ###### Parameters

    - `callback`: writes the response to this callback

    - `type`: the class of the business object returned

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

  - ### getAsJsonMap

    public ConnectionRequest getAsJsonMap(SuccessCallback<Response<Map>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

    ###### Deprecated

    use `#fetchAsJsonMap(com.codename1.util.OnComplete)` instead

  - ### getAsJsonMap

    public ConnectionRequest getAsJsonMap(SuccessCallback<Response<Map>> callback, FailureCallback<? extends Object> onError)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    - `onError`: the error callback

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

    ###### Deprecated

    use `#fetchAsJsonMap(com.codename1.util.OnComplete)` instead

  - ### getAsJsonMapAsync

    public void getAsJsonMapAsync(Callback<Response<Map>> callback)

    Executes the request asynchronously and writes the response to the provided Callback

    ###### Parameters

    - `callback`: writes the response to this callback

    ###### Deprecated

    use `#fetchAsJsonMap(com.codename1.util.OnComplete)` instead

  - ### getAsJsonMap

    public Response<Map> getAsJsonMap()

    Executes the request synchronously

    ###### Returns

    Response Object

  - ### getAsProperties

    public Response<PropertyBusinessObject> getAsProperties(Class type)

    Executes the request synchronously

    ###### Parameters

    - `type`: the type of the business object to create

    ###### Returns

    Response Object

  - ### fetchAsPropertyList

    public ConnectionRequest fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type, String root)

    Executes the request asynchronously and writes the response to the provided Callback. This fetches JSON data and parses it into a properties business object

    ###### Parameters

    - `callback`: writes the response to this callback

    - `type`: the class of the business object returned

    - `root`: the root element's key of the structured content

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

  - ### fetchAsPropertyList

    public ConnectionRequest fetchAsPropertyList(OnComplete<Response<List<PropertyBusinessObject>>> callback, Class type)

    Executes the request asynchronously and writes the response to the provided Callback. This fetches JSON data and parses it into a properties business object

    ###### Parameters

    - `callback`: writes the response to this callback

    - `type`: the class of the business object returned

    ###### Returns

    returns the Connection Request object so it can be killed if necessary

  - ### getAsPropertyList

    public Response<List<PropertyBusinessObject>> getAsPropertyList(Class type, String root)

    Executes the request synchronously

    ###### Parameters

    - `type`: the type of the business object to create

    - `root`: the root element's key of the structured content

    ###### Returns

    Response Object

  - ### getAsPropertyList

    public Response<List<PropertyBusinessObject>> getAsPropertyList(Class type)

    Executes the request synchronously

    ###### Parameters

    - `type`: the type of the business object to create

    ###### Returns

    Response Object

  - ### getRequestUrl

    public String getRequestUrl()