private void parseResponseBody() throws IOException { if (body != null) { String contentType = response.getHeader("Content-Type"); XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType); // skip parsing if we got text back (e.g. if we called _cat apis) if (xContentType == XContentType.JSON || xContentType == XContentType.YAML) { this.parsedResponse = ObjectPath.createFromXContent(xContentType.xContent(), body); } } }
/** * Parses the response body and extracts a specific value from it (identified by the provided * path) */ public Object evaluate(String path, Stash stash) throws IOException { if (response == null) { return null; } if (parsedResponse == null) { // special case: api that don't support body (e.g. exists) return true if 200, false if 404, // even if no body // is_true: '' means the response had no body but the client returned true (caused by 200) // is_false: '' means the response had no body but the client returned false (caused by 404) if ("".equals(path) && HttpHead.METHOD_NAME.equals(response.getRequestLine().getMethod())) { return isError() == false; } return null; } return parsedResponse.evaluate(path, stash); }
/** * Returns the body properly parsed depending on the content type. Might be a string or a json * object parsed as a map. */ public Object getBody() throws IOException { if (parsedResponse != null) { return parsedResponse.evaluate(""); } return body; }