DynamicResponse Class

  • java.lang.Object
    • com.azure.core.experimental.http.DynamicResponse

public final class 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")
     .setHttpMethod(HttpMethod.POST)
     .send(); // makes the service call

 // Check the HTTP status
 int statusCode = response.getStatusCode();
 if (statusCode == 200) {
     BinaryData responseBody = response.getBody();
     JsonObject deserialized = responseBody.toObject(JsonObject.class);
     int id = ((JsonNumber) deserialized.getProperty("id")).getValue().intValue();
     JsonArray tags = ((JsonArray) deserialized.getProperty("tags"));
     String firstTag = ((JsonString) ((JsonObject) tags.getElement(0)).getProperty("name")).getValue();
 }

Constructor Summary

Constructor Description
DynamicResponse(HttpResponse response, BinaryData body)

Creates an instance of the DynamicResponse.

Method Summary

Modifier and Type Method and Description
BinaryData getBody()

Returns the HTTP response body represented as a BinaryData.

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.

Methods inherited from java.lang.Object

Constructor Details

DynamicResponse

public DynamicResponse(HttpResponse response, BinaryData body)

Creates an instance of the DynamicResponse.

Parameters:

response - the underlying HTTP response
body - the full HTTP response body

Method Details

getBody

public BinaryData getBody()

Returns the HTTP response body represented as a BinaryData.

Returns:

the response body

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

getStatusCode

public int getStatusCode()

Returns the HTTP status code of the response.

Returns:

the HTTP status code of the response

Applies to