Package com.jcabi.http.response
Class JsonResponse
java.lang.Object
com.jcabi.http.response.JsonResponse
- All Implemented Interfaces:
Response
JSON response.
This response decorator is able to parse HTTP response body as a JSON document and manipulate with it afterwards, for example:
String name = new JdkRequest("http://my.example.com")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.fetch()
.as(JsonResponse.class)
.json()
.readObject()
.getString("name");
The class is immutable and thread-safe.
- Since:
- 0.8
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends Response>
TConvert it to another type, by encapsulation.assertJson(String element) Verifies the JSON data against the element identifier argument, and throwsAssertionErrorin case of mismatch.final Requestback()Get back to the request it's related to.final byte[]binary()Raw body as an array of bytes.body()Get body as a string, assuming it'sUTF-8(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).headers()Get a collection of all headers.jakarta.json.JsonReaderjson()Read body as JSON.final Stringreason()Get status line reason phrase.final intstatus()Get status of the response as a positive integer number.final StringtoString()
-
Constructor Details
-
JsonResponse
Public ctor.- Parameters:
resp- Response
-
-
Method Details
-
assertJson
Verifies the JSON data against the element identifier argument, and throwsAssertionErrorin case of mismatch.- Parameters:
element- Element in the JSON data of this object- Returns:
- This object
-
json
public jakarta.json.JsonReader json()Read body as JSON.- Returns:
- Json reader
-
toString
-
back
Description copied from interface:ResponseGet back to the request it's related to. -
status
public final int status()Description copied from interface:ResponseGet status of the response as a positive integer number. -
reason
Description copied from interface:ResponseGet status line reason phrase. -
headers
Description copied from interface:ResponseGet a collection of all headers. -
body
Description copied from interface:ResponseGet body as a string, assuming it'sUTF-8(if there is something else that can't be translated into a UTF-8 string a runtime exception will be thrown).DISCLAIMER: The only encoding supported here is UTF-8. If the body of response contains any chars that can't be used and should be replaced with a "replacement character", a
RuntimeExceptionwill be thrown. If you need to use some other encodings, useResponse.binary()instead. -
binary
public final byte[] binary()Description copied from interface:ResponseRaw body as an array of bytes. -
as
Description copied from interface:ResponseConvert it to another type, by encapsulation.
-