API Request Capabilities
Overview
This API request capability was introduced to provide a simple and efficient way to automate testing of RESTful APIs. It provides a fluent and intuitive API for setting up requests, sending them, and validating responses.
Key Features
- Ease of Use: Simple methods to configure and send API requests.
- Flexibility: Supports multiple media types and HTTP request methods.
- Scalability: Designed to be extendable for various test scenarios.
- Reusability: Encapsulation of request-building logic for cleaner test code.
Usage Guide
Class Overview
Request
: The main class for configuring and sending API requests.
Methods
Request.set(String url)
Sets the base URL for the API request.
- Parameter:
url
- The API endpoint (e.g.,https://api.example.com/resource
).
Request.setMediaType(String mediaType)
Specifies the media type of the request.
- Parameter:
mediaType
- The type of the media, such asapplication/json
orapplication/xml
.
Request.setHeader(String key, String value)
Adds a header to the request. You can set more than one set of headers using method multiple times.
- Parameters:
key
- The header name (e.g.,Authorization
).value
- The header value (e.g.,Bearer token
).
Request.setQueryParams(String key, String value)
Adds query parameters to the request URL. You can set more than one set of query parameters using method multiple times.
- Parameters:
key
- The query parameter name.value
- The query parameter value.
Request.setPayload(String payload)
Sets the request body.
- Parameter:
payload
- The payload content in string format (e.g., JSON or XML).
Request.send(String reqType)
Sends the API request using the specified HTTP method.
- Parameter:
reqType
- The HTTP method (e.g.,GET
,POST
,PUT
,DELETE
).
Request.response()
Retrieves the response object. This object contains the raw response returned by the API.
Request.getResponseString()
Returns the response content as a string.
Request.reset()
Resets the request object by closing all connections and clearing previous configurations.
Example Usage
// Setting up a GET request
Request.set("https://api.example.com/resource");
Request.setMediaType("application/json");
Request.setHeader("Authorization", "Bearer sample_token");
Request.setQueryParams("key1", "value1");
Request.setQueryParams("key2", "value2");
// Sending the request
Request.send(ReqType.GET);
// Retrieving and printing the response
String response = Request.getResponseString();
System.out.println("Response: " + response);
// Reset the request object
Request.reset();
Advanced Usage
Example: POST Request with JSON Payload
// Setting up a POST request
Request.set("https://api.example.com/resource");
Request.setMediaType("application/json");
Request.setHeader("Authorization", "Bearer sample_token");
Request.setPayload("{\"key\":\"value\"}");
// Sending the request
Request.send(ReqType.POST);
// Handling the response
if (Request.getStatusCode() == 200) {
System.out.println("Success: " + Request.getResponseString());
} else {
System.err.println("Error: " + Request.getStatusCode());
}
// Resetting the request object
Request.reset();
Fluent Request Approach
FluentRequest fluentRequest = new FluentRequest();
fluentRequest.set("url")
.setMediaType("application/json")
.setQueryParams("key", "value")
.setQueryParams("key2", "value2")
.setHeader("Authorization", "Bearer sample_token")
.setHeader("HeaderKey", "SomeValue")
.send(ReqType.POST);
System.out.println(fluentRequest.getStatusCode());
System.out.println(fluentRequest.getResponseString());
Error Handling
Common Errors and Solutions
Invalid URL
Ensure the URL passed toRequest.set()
is valid and includes the protocol (e.g.,https
).Unsupported Media Type
Check the media type passed toRequest.setMediaType()
. Supported types includeapplication/json
,application/xml
, etc.Missing Headers
Certain APIs require specific headers (e.g.,Authorization
). UseRequest.setHeader()
to include them.
Best Practices
Reuse Headers
If certain headers are common across multiple requests, encapsulate them in utility methods or configuration files.Payload Management
Use external JSON/XML files for complex payloads to maintain readability.Assertion Integration
Combine the framework with assertion libraries like TestNG or JUnit for automated validation of responses.Connection Reset
Always callRequest.reset()
after tests to avoid memory leaks or stale connections.
Extending the Framework
Adding Custom Methods
To add custom functionality, extend the Request
class or use utility classes to augment existing methods.
FAQs
Q: How to send auth information to your requests?
Auth information is generally sent via headers. For example if it is basic user name and password, it can be sent as key value pairs in the header. If it is a bearer token it can be sent using “Authorization”, “Bearer sample_token” format.
Q: Can I use this framework with CI/CD pipelines?
Yes, integrate it with tools like Jenkins, GitHub Actions, or Azure Pipelines for continuous testing.
Q: Does it support asynchronous requests?
Currently, the framework is designed for synchronous requests. Asynchronous support can be added as an extension.