Class 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 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 Detail

      • 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 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 a BinaryData.
        Returns:
        the response body