Package com.azure.core.experimental.http
Class DynamicResponse
java.lang.Object
com.azure.core.experimental.http.DynamicResponse
A response received from sending a DynamicRequest. This class enables inspecting the HTTP response status,
response headers and the response body. Response body is represented as a
BinaryData
which can then to
deserialized into a string representation, an object or just bytes. If the response is a JSON, then the string
representation will return the JSON.
To demonstrate how this class can be used to read the response, let's use Pet Store service as an example. The list of APIs available on this service are documented in the swagger definition.
Reading the response of a HTTP GET request to get a pet from a petId
The structure of the JSON response for the GET call is shown below:
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
This sample shows how to read the JSON response from the service and inspecting specific properties of the response.
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 // Check the HTTP status int statusCode = response.getStatusCode(); if (statusCode == 200) { BinaryData responseBody = response.getBody(); String responseBodyStr = responseBody.toString(); JsonObject deserialized = Json.createReader(new StringReader(responseBodyStr)).readObject(); int id = deserialized.getInt("id"); String firstTag = deserialized.getJsonArray("tags").get(0).asJsonObject().getString("name"); }
-
Constructor Summary
ConstructorsConstructorDescriptionDynamicResponse
(HttpResponse response, BinaryData body) Creates an instance of the DynamicResponse. -
Method Summary
Modifier and TypeMethodDescriptiongetBody()
Returns the HTTP response body represented as aBinaryData
.Returns the HTTP headers of the response.Returns the original HTTP request sent to the service.int
Returns the HTTP status code of the response.
-
Constructor Details
-
DynamicResponse
Creates an instance of the DynamicResponse.- Parameters:
response
- the underlying HTTP responsebody
- the full HTTP response body
-
-
Method Details
-
getStatusCode
public int getStatusCode()Returns the HTTP status code of the response.- Returns:
- the HTTP status code of the response
-
getHeaders
Returns the HTTP headers of the response.- Returns:
- the HTTP headers of the response
-
getRequest
Returns the original HTTP request sent to the service.- Returns:
- the original HTTP request sent to get this response
-
getBody
Returns the HTTP response body represented as aBinaryData
.- Returns:
- the response body
-