public final class DynamicResponse extends Object
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();javax.json.JsonObject
deserialized =javax.json.Json
.createReader(newStringReader
(responseBodyStr)).readObject(); int id = deserialized.getInt("id");String
firstTag = deserialized.getJsonArray("tags").get(0).asJsonObject().getString("name"); }
Constructor and Description |
---|
DynamicResponse(HttpResponse response,
BinaryData body)
Creates an instance of the DynamicResponse.
|
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.
|
public DynamicResponse(HttpResponse response, BinaryData body)
response
- the underlying HTTP responsebody
- the full HTTP response bodypublic int getStatusCode()
public HttpHeaders getHeaders()
public HttpRequest getRequest()
public BinaryData getBody()
BinaryData
.Copyright © 2021 Microsoft Corporation. All rights reserved.