Database
Pre-request operations are performed before the request is sent, allowing for custom scripts and database connections. These are commonly used for setting input parameters, printing data, etc.
Post-response operations take place after a request is sent and are typically used for setting variables, performing assertions, and similar tasks. They also support custom scripts and database connections.
EchoAPI offers a database connection feature, enabling developers to retrieve input parameters from a database or perform assertion verification during interface debugging.
EchoAPI supports connections to six types of databases: MySQL, SQL Server, Oracle, Clickhouse, PostgreSQL, and Redis.
Database-related operations can be executed in the Pre-request and Post-response phases of API debugging, cases, and testing.
How to Connect to a Database
How to Add a Database in Pre-request and Post-response
1.Add a Database
SELECT * FROM user LIMIT 5; // A standard simple query statement that retrieves 5 records from the user table.
2.Print Results
3.Extract Results to Variables
You can view the set variables from here
Common Database Scenarios
1. Retrieve data as request input parameters
First, extract variables from the database within the pre-request script.
Then use these variables for input values in your request.
Finally, click Send to execute the request.
2. Retrieve data for use in assertions
Primarily, add databases and scripts in the post-response phase.
Scenario: Assert whether the role
in the response content is correct.
pm.test("role is correct", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.args.role).to.eql(pm.environment.get("role_id"));
});
Add the database and set the values to be compared as variables
Add a script to verify whether the value of the response body matches the variable.
The assertion result failed because the actual returned email did not match the email in the database.
You can click the settings icon next to the environment in the upper right corner to view the data set as an environment variable.