Package com.azure.core.experimental.http
Class DynamicResponse
- java.lang.Object
-
- com.azure.core.experimental.http.DynamicResponse
-
public final class DynamicResponse extends Object
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 aBinaryData
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:
This sample shows how to read the JSON response from the service and inspecting specific properties of the response.{ "id": 0, "category": { "id": 0, "name": "string" }, "name": "doggie", "photoUrls": [ "string" ], "tags": [ { "id": 0, "name": "string" } ], "status": "available" }
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
Constructors Constructor Description DynamicResponse(HttpResponse response, BinaryData body)
Creates an instance of the DynamicResponse.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BinaryData
getBody()
Returns the HTTP response body represented as aBinaryData
.HttpHeaders
getHeaders()
Returns the HTTP headers of the response.HttpRequest
getRequest()
Returns the original HTTP request sent to the service.int
getStatusCode()
Returns the HTTP status code of the response.
-
-
-
Constructor Detail
-
DynamicResponse
public DynamicResponse(HttpResponse response, BinaryData body)
Creates an instance of the DynamicResponse.- Parameters:
response
- the underlying HTTP responsebody
- the full HTTP response body
-
-
Method Detail
-
getStatusCode
public int getStatusCode()
Returns the HTTP status code of the response.- Returns:
- the HTTP status code of the response
-
getHeaders
public HttpHeaders getHeaders()
Returns the HTTP headers of the response.- Returns:
- the HTTP headers of the response
-
getRequest
public HttpRequest getRequest()
Returns the original HTTP request sent to the service.- Returns:
- the original HTTP request sent to get this response
-
getBody
public BinaryData getBody()
Returns the HTTP response body represented as aBinaryData
.- Returns:
- the response body
-
-