DynamicRequest Class

  • java.lang.Object
    • com.azure.core.experimental.http.DynamicRequest

public final class DynamicRequest

This class facilitates constructing and sending a HTTP request. DynamicRequest can be used to configure the endpoint to which the request is sent, the request headers, path params, query params and the request body.

An instance of DynamicRequest can be created by either directly calling the constructor with an ObjectSerializer and HttpPipeline or obtained from a service client that preconfigures known components of the request like URL, path params etc.

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 DynamicRequest using the constructor

ObjectSerializer serializer = JsonSerializerProviders.createInstance(true);
 HttpPipeline pipeline = new HttpPipelineBuilder().build();
 DynamicRequest dynamicRequest = new DynamicRequest(serializer, pipeline);

An Azure service client may provide methods that are specific to the service which returns an instance DynamicRequest that comes preconfigured with some request components like the endpoint, required path params, headers etc.

Configuring the request with a path param and making a HTTP GET request

Continuing with the pet store example, getting information about a pet requires making a HTTP GET call to the pet service and setting the pet id in path param as shown in the sample below.

DynamicResponse response = dynamicRequest
     .setUrl("https://petstore.example.com/pet/{petId}") // may already be set if request is created from a client
     .setPathParam("petId", "2343245")
     .setHttpMethod(HttpMethod.POST)
     .send(); // makes the service call

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

To add a new pet to the pet store, a 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, {code azure-json} 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 DynamicRequest

DynamicResponse response = dynamicRequest
     .setUrl("https://petstore.example.com/pet") // may already be set if request is created from a client
     .addHeader(HttpHeaderName.CONTENT_TYPE, "application/json")
     .setBody(requestBodyData)
     .send(); // makes the service call

Constructor Summary

Constructor Description
DynamicRequest(ObjectSerializer objectSerializer, HttpPipeline httpPipeline)

Creates an instance of the Dynamic request.

Method Summary

Modifier and Type Method and Description
DynamicRequest addHeader(HttpHeader httpHeader)

Adds a header to the HTTP request

DynamicRequest addHeader(HttpHeaderName header, String value)

Adds a header to the HTTP request.

DynamicRequest addHeader(String header, String value)

Deprecated

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

Adds a header to the HTTP request.

DynamicRequest addQueryParam(String parameterName, String value)

Adds a query parameter to the request URL.

HttpPipeline getHttpPipeline()

Returns the HttpPipeline used for sending this request.

ObjectSerializer getObjectSerializer()

Returns the ObjectSerializer used for serializing this request.

DynamicResponse send()

Sends the request through the HTTP pipeline synchronously.

DynamicResponse send(Context context)

Sends the request through the HTTP pipeline synchronously.

reactor.core.publisher.Mono<DynamicResponse> sendAsync()

Sends the request through the HTTP pipeline asynchronously.

DynamicRequest setBody(Object body)

Sets the body on the HTTP request.

DynamicRequest setBody(String body)

Sets the string representation of the request body.

DynamicRequest setHeaders(HttpHeaders httpHeaders)

Sets the headers on the HTTP request.

DynamicRequest setHttpMethod(HttpMethod httpMethod)

Sets the HTTP method for this request.

DynamicRequest setPathParam(String parameterName, String value)

Sets the value for a specific path parameter in the URL.

DynamicRequest setUrl(String url)

Sets the URL for the HTTP request.

Methods inherited from java.lang.Object

Constructor Details

DynamicRequest

public DynamicRequest(ObjectSerializer objectSerializer, HttpPipeline httpPipeline)

Creates an instance of the Dynamic request. The objectSerializer provided to this constructor will be used to serialize the request and the httpPipeline configured with a series of Http pipeline policies will be applied before sending the request.

Parameters:

objectSerializer - a serializer for serializing and deserializing payloads
httpPipeline - the pipeline to send the actual HTTP request

Method Details

addHeader

public DynamicRequest addHeader(HttpHeader httpHeader)

Adds a header to the HTTP request

Parameters:

httpHeader - the header to add

Returns:

the modified DynamicRequest object

addHeader

public DynamicRequest addHeader(HttpHeaderName header, String value)

Adds a header to the HTTP request.

Parameters:

header - the header key
value - the header value

Returns:

the modified DynamicRequest object

addHeader

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

Deprecated

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

Adds a header to the HTTP request.

Parameters:

header - the header key
value - the header value

Returns:

the modified DynamicRequest object

addQueryParam

public DynamicRequest addQueryParam(String parameterName, String value)

Adds a query parameter to the request URL.

Parameters:

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

Returns:

the modified DynamicRequest object

getHttpPipeline

public HttpPipeline getHttpPipeline()

Returns the HttpPipeline used for sending this request.

Returns:

the pipeline to sending HTTP requests used by this DynamicRequest

getObjectSerializer

public ObjectSerializer getObjectSerializer()

Returns the ObjectSerializer used for serializing this request.

Returns:

the underlying serializer used by this DynamicRequest

send

public DynamicResponse send()

Sends the request through the HTTP pipeline synchronously.

Returns:

the dynamic response received from the API

send

public DynamicResponse send(Context context)

Sends the request through the HTTP pipeline synchronously.

Parameters:

context - the context to send with the request

Returns:

the dynamic response received from the API

sendAsync

public Mono<DynamicResponse> sendAsync()

Sends the request through the HTTP pipeline asynchronously.

Returns:

the reactor publisher for the dynamic response to subscribe to

setBody

public DynamicRequest setBody(Object body)

Sets the body on the HTTP request. The object is serialized using ObjectSerializer provided in the constructor of this request.

Parameters:

body - the body object that will be serialized

Returns:

the modified DynamicRequest object

setBody

public DynamicRequest setBody(String body)

Sets the string representation of the request body. The ObjectSerializer is not used if body is represented as string.

Parameters:

body - the serialized body content

Returns:

the modified DynamicRequest object

setHeaders

public DynamicRequest setHeaders(HttpHeaders httpHeaders)

Sets the headers on the HTTP request. This overwrites all existing HTTP headers for this request.

Parameters:

httpHeaders - the new headers to replace all existing headers

Returns:

the modified DynamicRequest object

setHttpMethod

public DynamicRequest setHttpMethod(HttpMethod httpMethod)

Sets the HTTP method for this request.

Parameters:

httpMethod - the HTTP method for the request

Returns:

the modified DynamicRequest object

setPathParam

public DynamicRequest setPathParam(String parameterName, String value)

Sets the value for a specific path parameter in the URL. The path parameter must be wrapped in a pair of curly braces, like "{paramName}".

Parameters:

parameterName - the path parameter's name in the curly braces
value - the String value to replace the path parameter

Returns:

the modified DynamicRequest object

setUrl

public DynamicRequest setUrl(String url)

Sets the URL for the HTTP request.

Parameters:

url - the URL for the request

Returns:

the modified DynamicRequest object

Applies to