public final class DynamicRequest extends Object
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 and Description |
---|
DynamicRequest(ObjectSerializer objectSerializer,
HttpPipeline httpPipeline)
Creates an instance of the Dynamic request.
|
Modifier and Type | Method and Description |
---|---|
DynamicRequest |
addHeader(HttpHeader httpHeader)
Adds a header to the HTTP request
|
DynamicRequest |
addHeader(String header,
String value)
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.
|
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.
|
public DynamicRequest(ObjectSerializer objectSerializer, HttpPipeline httpPipeline)
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.objectSerializer
- a serializer for serializing and deserializing payloadshttpPipeline
- the pipeline to send the actual HTTP requestNullPointerException
- if either of objectSerializer or httpPipeline is nullpublic ObjectSerializer getObjectSerializer()
ObjectSerializer
used for serializing this request.public HttpPipeline getHttpPipeline()
HttpPipeline
used for sending this request.public DynamicRequest setUrl(String url)
url
- the URL for the requestpublic DynamicRequest setHttpMethod(HttpMethod httpMethod)
httpMethod
- the HTTP method for the requestpublic DynamicRequest addHeader(String header, String value)
header
- the header keyvalue
- the header valuepublic DynamicRequest addHeader(HttpHeader httpHeader)
httpHeader
- the header to addNullPointerException
- if the httpHeader is nullpublic DynamicRequest setHeaders(HttpHeaders httpHeaders)
httpHeaders
- the new headers to replace all existing headerspublic DynamicRequest setBody(String body)
ObjectSerializer
is not used if body is
represented as string.body
- the serialized body contentpublic DynamicRequest setBody(Object body)
ObjectSerializer
provided in the
constructor of this request.body
- the body object that will be serializedUncheckedIOException
- if the body cannot be serializedpublic DynamicRequest setPathParam(String parameterName, String value)
parameterName
- the path parameter's name in the curly bracesvalue
- the String value to replace the path parameterIllegalArgumentException
- if the parameterName is not found in the endpoint URLpublic DynamicRequest addQueryParam(String parameterName, String value)
parameterName
- the name of the query parametervalue
- the value of the query parameterpublic DynamicResponse send()
public DynamicResponse send(Context context)
context
- the context to send with the requestpublic Mono<DynamicResponse> sendAsync()
Copyright © 2021 Microsoft Corporation. All rights reserved.