Class RestActions
java.lang.Object
com.shaft.api.RestActions
Core REST API automation engine for SHAFT, built on top of REST Assured.
This class handles HTTP request construction, execution, response parsing, and validation for RESTful web services. It supports GET, POST, PUT, PATCH, and DELETE methods, along with GraphQL queries.
For new code, prefer using SHAFT.API which
provides a cleaner, session-scoped fluent interface. This class remains
available for legacy compatibility and for its static utility methods
such as getResponseJSONValue(Response, String).
Usage example (via SHAFT.API):
SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/posts/1").setTargetStatusCode(200).performRequest();
String title = api.getResponseJSONValue("$.title");
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumComparison strategy used when validating a response body against a reference JSON file.static enumDefines how request parameters are encoded and transmitted.static enumHTTP methods supported for building API requests. -
Constructor Summary
ConstructorsConstructorDescriptionRestActions(String serviceURI) RestActions(String serviceURI, SHAFT.API driver) -
Method Summary
Modifier and TypeMethodDescriptionaddCookieVariable(String key, String value) Appends a cookie to every subsequent request in this session.addHeaderVariable(String key, String value) Append a header to the current session to be used in all the following requests.buildNewRequest(String serviceName, RestActions.RequestType requestType) Builds a new API request within this session, inheriting all session-level headers, cookies, and configuration.static RequestBuilderbuildNewRequest(String serviceURI, String serviceName, RestActions.RequestType requestType) Builds a new API request using a static factory approach.static booleancompareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType) Compares the Response object against the content of the referenceJsonFilePathstatic booleancompareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType, String jsonPathToTargetArray) Compares the Response object against the content of the referenceJsonFilePathstatic StringPretty-prints an XML string by re-formatting it with consistent indentation.io.restassured.response.Responsestatic StringgetResponseBody(io.restassured.response.Response response) Extracts the response body and returns it as a plain stringstatic StringgetResponseJSONValue(io.restassured.response.Response response, String jsonPath) Extracts a string value from the response body by parsing the target jsonpathstatic StringgetResponseJSONValue(Object response, String jsonPath) Extracts a string value from an arbitrary response object by evaluating a JSONPath expression.getResponseJSONValueAsList(io.restassured.response.Response response, String jsonPath) Extracts a list of values from the response body by evaluating a JSONPath expression.static StringgetResponseJSONValueFromList(io.restassured.response.Response response, String jsonPathToList, String jsonPathToValueNeeded, String jsonPathToValueReference, String valueReference) Extracts a string value from an object of a list by reference of another attribute inside the same objectstatic intgetResponseStatusCode(io.restassured.response.Response response) Returns the HTTP status code of the given API response and logs it.static longgetResponseTime(io.restassured.response.Response response) Returns the total elapsed time of the given API response in milliseconds.static StringgetResponseXMLValue(io.restassured.response.Response response, String xmlPath) Extracts a string value from the XML response body using the given XPath expression.static StringgetResponseXMLValue(Object response, String xmlPath) Extracts a string attribute value from an XMLNodeobject.getResponseXMLValueAsList(io.restassured.response.Response response, String xmlPath) Extracts a list of XMLNodechildren matching the given XPath expression.static InputStreamparseBodyToJson(io.restassured.response.Response response) Parses a REST AssuredResponsebody into a JSONInputStream.static InputStreamparseBodyToJson(Object body) Converts an arbitrary response body object to a JSONInputStream.static io.restassured.response.ResponsesendGraphQlRequest(String base_URI, String query) Perform Graphql Request using Query - WITHOUT Header.static io.restassured.response.ResponsesendGraphQlRequest(String base_URI, String query, String variables) Perform Graphql Request using Query and Variables - WITHOUT Header.static io.restassured.response.ResponsesendGraphQlRequest(String base_URI, String query, String variables, String fragment) Perform Graphql Request using Query, Variables, and Fragments - WITHOUT Header.static io.restassured.response.ResponsesendGraphQlRequestWithHeader(String base_URI, String query, String header_key, String header_value) Perform Graphql Request using Query - WITH Header.static io.restassured.response.ResponsesendGraphQlRequestWithHeader(String base_URI, String query, String variables, String header_key, String header_value) Perform Graphql Request using Query and Variables - WITH Header.static io.restassured.response.ResponsesendGraphQlRequestWithHeader(String base_URI, String query, String variables, String fragment, String header_key, String header_value) Perform Graphql Request using Query, Variables, and Fragments - WITH Header.
-
Constructor Details
-
RestActions
-
RestActions
-
-
Method Details
-
buildNewRequest
public static RequestBuilder buildNewRequest(String serviceURI, String serviceName, RestActions.RequestType requestType) Builds a new API request using a static factory approach. Prefer usingSHAFT.APIand its instance-levelbuildNewRequest(String, RequestType)for new tests.- Parameters:
serviceURI- the base URI of the service (e.g."https://api.example.com/")serviceName- the endpoint path (e.g."users/1")requestType- the HTTP method to use (e.g.RestActions.RequestType.GET)- Returns:
- a
RequestBuilderready for further configuration and execution - See Also:
-
parseBodyToJson
Parses a REST AssuredResponsebody into a JSONInputStream. Delegates toparseBodyToJson(Object)using the response's raw body.- Parameters:
response- the API response whose body will be parsed- Returns:
- an
InputStreamcontaining the JSON-formatted body
-
parseBodyToJson
Converts an arbitrary response body object to a JSONInputStream. If the object cannot be serialized as JSON, it is serialized using Java object serialization, or returned as a raw byte stream for REST-Assured response bodies.- Parameters:
body- the response body object to convert — may be a REST-AssuredResponse, aString, or any serializable Java object- Returns:
- an
InputStreamrepresenting the body content
-
getResponseBody
Extracts the response body and returns it as a plain string- Parameters:
response- the target API response object- Returns:
- a string value that represents the response body
-
getResponseJSONValue
public static String getResponseJSONValue(io.restassured.response.Response response, String jsonPath) Extracts a string value from the response body by parsing the target jsonpath- Parameters:
response- the full response object returned by 'performRequest()' methodjsonPath- the JSONPath expression that will be evaluated in order to extract the desired value [without the trailing $.], please refer to these urls for examples: https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html http://jsonpath.com/- Returns:
- a string value that contains the extracted object
-
getResponseJSONValue
Extracts a string value from an arbitrary response object by evaluating a JSONPath expression. Supports REST-AssuredResponse,String,JSONObject,JSONArray,List, andHashMapinputs.- Parameters:
response- the response object to parse — not limited to REST-AssuredResponsejsonPath- the JSONPath expression (e.g."$.id"or"id")- Returns:
- the extracted string value, or
nullif not found - See Also:
-
getResponseJSONValueAsList
public static List<Object> getResponseJSONValueAsList(io.restassured.response.Response response, String jsonPath) Extracts a list of values from the response body by evaluating a JSONPath expression.- Parameters:
response- the REST-Assured response objectjsonPath- the JSONPath expression that resolves to an array (e.g."$.items[*].id")- Returns:
- a
Listof matched objects, ornullif none are found - See Also:
-
getResponseJSONValueFromList
public static String getResponseJSONValueFromList(io.restassured.response.Response response, String jsonPathToList, String jsonPathToValueNeeded, String jsonPathToValueReference, String valueReference) Extracts a string value from an object of a list by reference of another attribute inside the same object- Parameters:
response- The target API response objectjsonPathToList- The JSON path to the list of object inside the full responsejsonPathToValueNeeded- The JSON path to the attribute value you need to extract inside an object from the list. for example: idjsonPathToValueReference- The JSON path that refers to the needed attribute value inside an object from the list. for example: usernamevalueReference- The attribute value of the reference JSON path- Returns:
- A string value from the object of the list which represents the first match of the reference attribute value
-
getResponseXMLValue
Extracts a string value from the XML response body using the given XPath expression.- Parameters:
response- the REST-Assured response object with an XML bodyxmlPath- the XPath expression (e.g."root.item.name")- Returns:
- the extracted string value, or
nullif not found - See Also:
-
getResponseXMLValue
Extracts a string attribute value from an XMLNodeobject.- Parameters:
response- an XMLNode(typically obtained fromgetResponseXMLValueAsList(Response, String))xmlPath- the attribute name to retrieve from the node- Returns:
- the attribute value string, or
nullif not found
-
getResponseXMLValueAsList
-
getResponseStatusCode
public static int getResponseStatusCode(io.restassured.response.Response response) Returns the HTTP status code of the given API response and logs it.- Parameters:
response- the REST-Assured response object- Returns:
- the HTTP status code (e.g.
200,404)
-
getResponseTime
public static long getResponseTime(io.restassured.response.Response response) Returns the total elapsed time of the given API response in milliseconds.- Parameters:
response- the REST-Assured response object- Returns:
- the response time in milliseconds
-
compareJSON
public static boolean compareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType) Compares the Response object against the content of the referenceJsonFilePath- Parameters:
response- the full response object returned by performRequest method.referenceJsonFilePath- the full absolute path to the test data file that will be used as a reference for this comparisoncomparisonType- ComparisonType.EQUALS, CONTAINS, MATCHES, EQUALS_STRICT; Note that MATCHES ignores the content ordering inside the JSON- Returns:
- a boolean value that is TRUE in case the comparison passed, or FALSE in case it failed
-
compareJSON
public static boolean compareJSON(io.restassured.response.Response response, String referenceJsonFilePath, RestActions.ComparisonType comparisonType, String jsonPathToTargetArray) Compares the Response object against the content of the referenceJsonFilePath- Parameters:
response- the full response object returned by performRequest method.referenceJsonFilePath- the full absolute path to the test data file that will be used as a reference for this comparisoncomparisonType- ComparisonType.EQUALS, CONTAINS; Note that MATCHES ignores the content ordering inside the JSONjsonPathToTargetArray- a jsonpath that will be parsed to point to the target JSON Array- Returns:
- a boolean value that is TRUE in case the comparison passed, or FALSE in case it failed
-
formatXML
-
sendGraphQlRequest
Perform Graphql Request using Query - WITHOUT Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.- Returns:
- Graphql Response
-
sendGraphQlRequest
public static io.restassured.response.Response sendGraphQlRequest(String base_URI, String query, String variables) Perform Graphql Request using Query and Variables - WITHOUT Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.variables- graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variables- Returns:
- Graphql Response
-
sendGraphQlRequest
public static io.restassured.response.Response sendGraphQlRequest(String base_URI, String query, String variables, String fragment) Perform Graphql Request using Query, Variables, and Fragments - WITHOUT Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.variables- graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variablesfragment- graphql fragment; reusable units let you construct sets of fields, and then include them in queries where you need to. please refer to this url for examples:: https://graphql.org/learn/queries/#fragments- Returns:
- Graphql Response
-
getResponse
public io.restassured.response.Response getResponse() -
sendGraphQlRequestWithHeader
public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String header_key, String header_value) Perform Graphql Request using Query - WITH Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.header_key- the name of the header that you want to add. example:: "Authorization"header_value- the value that will be put inside the key. example:: "bearer ${token}"- Returns:
- Graphql Response
-
sendGraphQlRequestWithHeader
public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String variables, String header_key, String header_value) Perform Graphql Request using Query and Variables - WITH Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.variables- graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variablesheader_key- the name of the header that you want to add. example:: "Authorization"header_value- the value that will be put inside the key. example:: "bearer ${token}"- Returns:
- Graphql Response
-
sendGraphQlRequestWithHeader
public static io.restassured.response.Response sendGraphQlRequestWithHeader(String base_URI, String query, String variables, String fragment, String header_key, String header_value) Perform Graphql Request using Query, Variables, and Fragments - WITH Header.- Parameters:
base_URI- The Base URI without "graphql". example:: "https://api.example.com/"query- graphql query or mutation.variables- graphql variables; dynamic values of the query. please refer to this url for examples:: https://graphql.org/learn/queries/#variablesfragment- graphql fragment; reusable units let you construct sets of fields, and then include them in queries where you need to. please refer to this url for examples:: https://graphql.org/learn/queries/#fragmentsheader_key- the name of the header that you want to add. example:: "Authorization"header_value- the value that will be put inside the key. example:: "bearer ${token}"- Returns:
- Graphql Response
-
buildNewRequest
Builds a new API request within this session, inheriting all session-level headers, cookies, and configuration.- Parameters:
serviceName- the endpoint path relative to this instance's base URIrequestType- the HTTP method to use (e.g.RestActions.RequestType.POST)- Returns:
- a
RequestBuilderfor configuring and executing the request - See Also:
-
addHeaderVariable
Append a header to the current session to be used in all the following requests. Note: This feature is commonly used for authentication tokens.- Parameters:
key- the name of the header that you want to addvalue- the value that will be put inside the key- Returns:
- self-reference to be used for chaining actions
-
addCookieVariable
Appends a cookie to every subsequent request in this session.- Parameters:
key- the cookie namevalue- the cookie value- Returns:
- self-reference to be used for chaining actions
-