DynamicRequest Class
- java.
lang. Object - com.
azure. core. experimental. http. DynamicRequest
- com.
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 |
|---|---|
|
Dynamic |
addHeader(HttpHeader httpHeader)
Adds a header to the HTTP request |
|
Dynamic |
addHeader(HttpHeaderName header, String value)
Adds a header to the HTTP request. |
|
Dynamic |
addHeader(String header, String value)
Deprecated
Use addHeader(HttpHeaderName header, String value) as it provides better performance.
Adds a header to the HTTP request. |
|
Dynamic |
addQueryParam(String parameterName, String value)
Adds a query parameter to the request URL. |
|
Http |
getHttpPipeline()
Returns the HttpPipeline used for sending this request. |
|
Object |
getObjectSerializer()
Returns the ObjectSerializer used for serializing this request. |
|
Dynamic |
send()
Sends the request through the HTTP pipeline synchronously. |
|
Dynamic |
send(Context context)
Sends the request through the HTTP pipeline synchronously. |
|
reactor.core.publisher.Mono<Dynamic |
sendAsync()
Sends the request through the HTTP pipeline asynchronously. |
|
Dynamic |
setBody(Object body)
Sets the body on the HTTP request. |
|
Dynamic |
setBody(String body)
Sets the string representation of the request body. |
|
Dynamic |
setHeaders(HttpHeaders httpHeaders)
Sets the headers on the HTTP request. |
|
Dynamic |
setHttpMethod(HttpMethod httpMethod)
Sets the HTTP method for this request. |
|
Dynamic |
setPathParam(String parameterName, String value)
Sets the value for a specific path parameter in the URL. |
|
Dynamic |
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:
Method Details
addHeader
public DynamicRequest addHeader(HttpHeader httpHeader)
Adds a header to the HTTP request
Parameters:
Returns:
addHeader
public DynamicRequest addHeader(HttpHeaderName header, String value)
Adds a header to the HTTP request.
Parameters:
Returns:
addHeader
@Deprecated
public DynamicRequest addHeader(String header, String value)
Deprecated
Adds a header to the HTTP request.
Parameters:
Returns:
addQueryParam
public DynamicRequest addQueryParam(String parameterName, String value)
Adds a query parameter to the request URL.
Parameters:
Returns:
getHttpPipeline
public HttpPipeline getHttpPipeline()
Returns the HttpPipeline used for sending this request.
Returns:
getObjectSerializer
public ObjectSerializer getObjectSerializer()
Returns the ObjectSerializer used for serializing this request.
Returns:
send
public DynamicResponse send()
Sends the request through the HTTP pipeline synchronously.
Returns:
send
public DynamicResponse send(Context context)
Sends the request through the HTTP pipeline synchronously.
Parameters:
Returns:
sendAsync
public Mono<DynamicResponse> sendAsync()
Sends the request through the HTTP pipeline asynchronously.
Returns:
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:
Returns:
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:
Returns:
setHeaders
public DynamicRequest setHeaders(HttpHeaders httpHeaders)
Sets the headers on the HTTP request. This overwrites all existing HTTP headers for this request.
Parameters:
Returns:
setHttpMethod
public DynamicRequest setHttpMethod(HttpMethod httpMethod)
Sets the HTTP method for this request.
Parameters:
Returns:
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:
Returns:
setUrl
public DynamicRequest setUrl(String url)
Sets the URL for the HTTP request.
Parameters:
Returns: