How to Debug an API to Ensure Data Consistency with the Database

ยท

3 min read

Introduction

Database

After we complete writing an API, it's often necessary to perform debugging. In this process, we may encounter two specific scenarios:

  • Fetching data from existing tables to use as request parameters for the current API.

  • After sending the request, compare the API response with the database data to ensure consistency.

In my previous API debugging experiences, I frequently used Postman. However, I found it lacked certain functionalities to meet these specific needs until I discovered EchoAPI as the optimal replacement.

EchoAPI is fully compatible with Postman's scripting syntax, allowing me to seamlessly migrate my old data from Postman. ๐Ÿš€โœจ

How to extract parameter data from a database using EchoAPI

EchoAPI facilitates database connectivity, empowering developers to leverage databases for parameter retrieval and assertion checks during interface debugging.

With support for 6 database types such as MySQL, SQL Server, Oracle, Clickhouse, PostgreSQL and Redis, EchoAPI offers a versatile range of database connection options.

EchoAPI enables database operations in various scenarios, including API debugging, interface test cases, pre-execution of automated tests, and execution of post-execution scripts, enhancing the efficiency and effectiveness of testing processes.

Configure Database

In the Settings - Public Resources - Database , create a database entry.

Configure Database

After filling out the details, click on Test connection to test the database.

Test connection

Applying Database Data to Pre-request and Post-response

Following the database configuration, users can integrate database scripts into the Pre-request and Post-response sections.

Applying Database Data to Pre-request and Post-response

Upon entering SQL statements in the database script, users can designate the results as variables and specify the variable type for configuration.

Applying Database Data to Pre-request and Post-response

Once the console printing of results is enabled, users can view the execution outcomes of SQL statements in the console. Based on the console's output, JSONPath expressions can be employed to retrieve variable values.

Applying Database Data to Pre-request and Post-response

Example:
Create environment variables named "id" and "name" and use 'JSONPath' to extract the "id" and "name" fields from the first record of the database.

Applying Database Data to Pre-request and Post-response

Next, in the raw JSON parameter, reference the data just retrieved using {{id}} {{name}} and send the API request.

Applying Database Data to Pre-request and Post-response

You can observe that the request parameters have been successfully retrieved and utilized from the data extracted from the database.

Applying Database Data to Pre-request and Post-response

Comparing API Responses with Database Data Using Visual Assertions

Next, we will extract the fourth set of data from the database where the name = Mata in the Post-response.

Applying Database Data to Pre-request and Post-response

Next, add an 'Assertion' to retrieve the value of the returned name and compare it to the name extracted from the database to check for equality. The database name is represented as '{{name}}'.

Applying Database Data to Pre-request and Post-response

Send the request and view the results.

Applying Database Data to Pre-request and Post-response

Conclusion

EchoAPI stands out as a transformative solution in API debugging, effectively tackling key developer challenges. With its seamless database connectivity, diverse database options, and streamlined testing processes, EchoAPI simplifies parameter data extraction and assertion checks during debugging. Its compatibility with Postman's scripting syntax makes the transition seamless, providing developers with a comprehensive tool to ensure effortless data consistency between APIs and databases.
With versatile, user-friendly features and robust functionality, EchoAPI enhances API performance, streamlines debugging workflows, and improves software reliability. Embrace EchoAPI to unlock greater efficiency and precision in API development and debugging processes.

Try EchoAPI Interceptor today!

https://www.echoapi.com/plugin/chrome?utm_source=6715d073

ย