Class DynamicRequest

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

public final class DynamicRequest extends Object
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")
     .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, 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 = Json.createArrayBuilder()
     .add("https://imgur.com/pet1")
     .add("https://imgur.com/pet2")
     .build();

 JsonArray tags = Json.createArrayBuilder()
     .add(Json.createObjectBuilder()
         .add("id", 0)
         .add("name", "Labrador")
         .build())
     .add(Json.createObjectBuilder()
         .add("id", 1)
         .add("name", "2021")
         .build())
     .build();

 JsonObject requestBody = Json.createObjectBuilder()
     .add("id", 0)
     .add("name", "foo")
     .add("status", "available")
     .add("category", Json.createObjectBuilder().add("id", 0).add("name", "dog"))
     .add("photoUrls", photoUrls)
     .add("tags", tags)
     .build();

 String requestBodyStr = requestBody.toString();
 
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("Content-Type", "application/json")
     .setBody(requestBodyStr)
     .send(); // makes the service call
 
  • 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
      Throws:
      NullPointerException - if either of objectSerializer or httpPipeline is null
  • Method Details

    • getObjectSerializer

      public ObjectSerializer getObjectSerializer()
      Returns the ObjectSerializer used for serializing this request.
      Returns:
      the underlying serializer used by this DynamicRequest
    • getHttpPipeline

      public HttpPipeline getHttpPipeline()
      Returns the HttpPipeline used for sending this request.
      Returns:
      the pipeline to sending HTTP requests used by this DynamicRequest
    • 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
    • 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
    • addHeader

      public DynamicRequest addHeader(String 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

      public DynamicRequest addHeader(HttpHeader httpHeader)
      Adds a header to the HTTP request
      Parameters:
      httpHeader - the header to add
      Returns:
      the modified DynamicRequest object
      Throws:
      NullPointerException - if the httpHeader is null
    • 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
    • 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
    • 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
      Throws:
      UncheckedIOException - if the body cannot be serialized
    • 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
      Throws:
      IllegalArgumentException - if the parameterName is not found in the endpoint URL
    • 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
    • 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