Skip to main content

Database

tip

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

View here

How to Add a Database in Pre-request and Post-response

1.Add a Database

image.png

SELECT * FROM user LIMIT 5; // A standard simple query statement that retrieves 5 records from the user table.

2.Print Results

image.png

3.Extract Results to Variables

image.png

You can view the set variables from here

image.png

Common Database Scenarios

1. Retrieve data as request input parameters

First, extract variables from the database within the pre-request script.

image.png

Then use these variables for input values in your request.

image.png

Finally, click Send to execute the request.

image.png

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

image.png

Add a script to verify whether the value of the response body matches the variable.

image.png

The assertion result failed because the actual returned email did not match the email in the database.

image.png

You can click the settings icon next to the environment in the upper right corner to view the data set as an environment variable.

image.png