Share via


RequestOptions Class

  • java.lang.Object
    • com.azure.core.http.rest.RequestOptions

public final class RequestOptions

This class contains the options to customize an HTTP request. RequestOptions can be used to configure the request headers, query params, the request body, or add a callback to modify all aspects of the HTTP request.

An instance of fully configured RequestOptions can be passed to a service method that preconfigures known components of the request like URL, path params etc, further modifying both un-configured, or preconfigured components.

To demonstrate how this class can be used to construct a request, let's use a Pet Store service as an example. The list of APIs available on this service are documented in the swagger definition.

Creating an instance of RequestOptions

RequestOptions options = new RequestOptions()
     .setBody(BinaryData.fromString("{\"name\":\"Fluffy\"}"))
     .addHeader("x-ms-pet-version", "2021-06-01");

Configuring the request with JSON body and making a HTTP POST request

To add a new pet to the pet store, an HTTP POST call should be made to the service with the details of the pet that is to be added. The details of the pet are included as the request body in JSON format. The JSON structure for the request is defined as follows:

{
   "id": 0,
   "category": {
     "id": 0,
     "name": "string"
 },
 "name": "doggie",
 "photoUrls": [
 "string"
 ],
 "tags": [
 {
 "id": 0,
 "name": "string"
 }
 ],
 "status": "available"
 }

To create a concrete request, Json builder provided in javax package is used here for demonstration. However, any other Json building library can be used to achieve similar results.

JsonArray photoUrls = new JsonArray()
     .addElement(new JsonString("https://imgur.com/pet1"))
     .addElement(new JsonString("https://imgur.com/pet2"));

 JsonArray tags = new JsonArray()
     .addElement(new JsonObject()
         .setProperty("id", new JsonNumber(0))
         .setProperty("name", new JsonString("Labrador")))
     .addElement(new JsonObject()
         .setProperty("id", new JsonNumber(1))
         .setProperty("name", new JsonString("2021")));

 JsonObject requestBody = new JsonObject()
     .setProperty("id", new JsonNumber(0))
     .setProperty("name", new JsonString("foo"))
     .setProperty("status", new JsonString("available"))
     .setProperty("category", new JsonObject()
         .setProperty("id", new JsonNumber(0))
         .setProperty("name", new JsonString("dog")))
     .setProperty("photoUrls", photoUrls)
     .setProperty("tags", tags);

 BinaryData requestBodyData = BinaryData.fromObject(requestBody);

Now, this string representation of the JSON request can be set as body of RequestOptions

RequestOptions options = new RequestOptions()
     .addRequestCallback(request -> request
         // may already be set if request is created from a client
         .setUrl("https://petstore.example.com/pet")
         .setHttpMethod(HttpMethod.POST)
         .setBody(requestBodyData)
         .setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"));

Constructor Summary

Constructor Description
RequestOptions()

Creates a new instance of RequestOptions.

Method Summary

Modifier and Type Method and Description
RequestOptions addHeader(HttpHeaderName header, String value)

Adds a header to the HTTP request.

RequestOptions addHeader(String header, String value)

Deprecated

Use addHeader(HttpHeaderName header, String value) as it provides better performance.

Adds a header to the HTTP request.

RequestOptions addQueryParam(String parameterName, String value)

Adds a query parameter to the request URL.

RequestOptions addQueryParam(String parameterName, String value, boolean encoded)

Adds a query parameter to the request URL, specifying whether the parameter is already encoded.

RequestOptions addRequestCallback(Consumer<HttpRequest> requestCallback)

Adds a custom request callback to modify the HTTP request before it's sent by the HttpClient.

Context getContext()

Gets the additional context on the request that is passed during the service call.

RequestOptions setBody(BinaryData requestBody)

Sets the body to send as part of the HTTP request.

RequestOptions setContext(Context context)

Sets the additional context on the request that is passed during the service call.

RequestOptions setHeader(HttpHeaderName header, String value)

Sets a header on the HTTP request.

RequestOptions setHeader(String header, String value)

Deprecated

Use setHeader(HttpHeaderName header, String value) as it provides better performance.

Sets a header on the HTTP request.

Methods inherited from java.lang.Object

Constructor Details

RequestOptions

public RequestOptions()

Creates a new instance of RequestOptions.

Method Details

addHeader

public RequestOptions addHeader(HttpHeaderName header, String value)

Adds a header to the HTTP request.

If a header with the given name exists the value is added to the existing header (comma-separated), otherwise a new header is created.

Parameters:

header - the header key
value - the header value

Returns:

the modified RequestOptions object

addHeader

@Deprecated
public RequestOptions addHeader(String header, String value)

Deprecated

Use addHeader(HttpHeaderName header, String value) as it provides better performance.

Adds a header to the HTTP request.

If a header with the given name exists the value is added to the existing header (comma-separated), otherwise a new header is created.

Parameters:

header - the header key
value - the header value

Returns:

the modified RequestOptions object

addQueryParam

public RequestOptions addQueryParam(String parameterName, String value)

Adds a query parameter to the request URL. The parameter name and value will be URL encoded. To use an already encoded parameter name and value, call addQueryParam("name", "value", true).

Parameters:

parameterName - the name of the query parameter
value - the value of the query parameter

Returns:

the modified RequestOptions object

addQueryParam

public RequestOptions addQueryParam(String parameterName, String value, boolean encoded)

Adds a query parameter to the request URL, specifying whether the parameter is already encoded. A value true for this argument indicates that value of value() is already encoded hence engine should not encode it, by default value will be encoded.

Parameters:

parameterName - the name of the query parameter
value - the value of the query parameter
encoded - whether this query parameter is already encoded

Returns:

the modified RequestOptions object

addRequestCallback

public RequestOptions addRequestCallback(Consumer<HttpRequest> requestCallback)

Adds a custom request callback to modify the HTTP request before it's sent by the HttpClient. The modifications made on a RequestOptions object is applied in order on the request.

Parameters:

requestCallback - the request callback

Returns:

the modified RequestOptions object

getContext

public Context getContext()

Gets the additional context on the request that is passed during the service call.

Returns:

The additional context that is passed during the service call.

setBody

public RequestOptions setBody(BinaryData requestBody)

Sets the body to send as part of the HTTP request.

Parameters:

requestBody - the request body data

Returns:

the modified RequestOptions object

setContext

public RequestOptions setContext(Context context)

Sets the additional context on the request that is passed during the service call.

Parameters:

context - Additional context that is passed during the service call.

Returns:

the modified RequestOptions object

setHeader

public RequestOptions setHeader(HttpHeaderName header, String value)

Sets a header on the HTTP request.

If a header with the given name exists it is overridden by the new value.

Parameters:

header - the header key
value - the header value

Returns:

the modified RequestOptions object

setHeader

@Deprecated
public RequestOptions setHeader(String header, String value)

Deprecated

Use setHeader(HttpHeaderName header, String value) as it provides better performance.

Sets a header on the HTTP request.

If a header with the given name exists it is overridden by the new value.

Parameters:

header - the header key
value - the header value

Returns:

the modified RequestOptions object

Applies to