You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
14 Hrs
27 Min
43 Seg
Curso de Backend con ExpressJS

Curso de Backend con ExpressJS

Oscar Barajas Tavares

Oscar Barajas Tavares

Postman

6/30
Resources

Creating robust APIs requires not only programming skills, but also the right tools to test and document them. Postman has become an indispensable ally for developers looking to streamline their workflow when building, testing and sharing APIs. In this content, we will explore how to configure Postman to test endpoints in Express.js, a fundamental skill for any backend developer.

Why is it important to install Postman for API development?

Postman is a powerful tool that goes beyond simply testing endpoints. It allows you to document your APIs and create complete resources that you can share with your team. This makes collaboration much easier and ensures that everyone understands how the API you are building works.

There are two main ways to use Postman:

  • From the browser with the agent installed (to listen for changes on localhost).
  • Installing the application directly on your computer

Proper documentation of your APIs is crucial for both your day-to-day professional development and the projects you build. A well-documented API is easier to maintain, scale and share with other developers.

How to create a workspace in Postman for your projects?

To organize your API tests, it is advisable to create a dedicated workspace:

  1. Go to the "Workspace" section in Postman.
  2. Select "Create Workspace".
  3. Leave the configuration blank initially
  4. Give it a descriptive name (e.g. "curso-express-js")
  5. Set it as personal if you don't need collaboration

It is important to mention that Postman offers free functionality for personal use, although it has paid options for collaborative work in teams. For individual projects, the free version is more than enough.

How to set up requests to test endpoints in Express?

There are several ways to create requests in Postman:

  • Manually, configuring each request one by one.
  • Importing a JSON file with the configuration of the requests
  • By using artificial intelligence to generate the JSON file based on the description of your endpoints.

For our case study, we are going to focus on testing two specific endpoints:

  1. An endpoint to process form data(/form).
  2. An endpoint to receive JSON data(/api/data)
// Configuration for the form endpoint// POST /form// Content-Type: application/x-www-form-urlencoded{ " name": "John Tavares", " email": "[email protected]"}
// Configuration for the JSON data endpoint// POST /api/data// Content-Type: application/json{ " key1": "value1", " key2": "value2"}

How to test endpoints that receive different data types?

Testing the endpoint for forms

When testing the endpoint for forms, it is crucial to verify that the field names exactly match what your server expects. In our example:

  1. We set up the POST method
  2. Set the correct URL(http://localhost:3005/form)
  3. Set the Content-Type to "form-data" or "x-www-form-urlencoded".
  4. Add the fields "name" and "email" with their values.

It is important to note the difference between the field names in the interface and in the programming logic. While in the interface we can use "nombre" (in Spanish), in the backend code we could be using "name" (in English). This distinction is allowed, although the ideal is to maintain consistency using English throughout the code.

When sending the request, we should receive a successful response (code 200 or 201) with the processed data.

Testing the endpoint for JSON data

For the endpoint that receives JSON data:

  1. We set up the POST method
  2. Set the correct URL(http://localhost:3005/api/data)
  3. Set the Content-Type to "application/json".
  4. Add a JSON object in the body of the request
{ " key1": "value1", " key2": "value2"}

When sending this request, we should receive a response with code 201 (Created) and an object confirming the data received.

Why is it important to document your APIs on GitHub?

Documentation is an essential part of API development. Not only does it help other developers understand how to use your API, but it also helps yourself when you need to revise it in the future.

It is recommended:

  • Save your Postman configurations.
  • Document each endpoint with examples of request and response.
  • Keep the documentation updated as your API evolves
  • Share the Postman configuration file along with your code.

This greatly facilitates collaboration and ensures that anyone working with your API can quickly understand it.

Proper use of tools like Postman not only improves your workflow as a developer, but also raises the quality of your APIs by ensuring they are well tested and documented. Have you used Postman in your projects? Share in the comments how you organize your API testing and what documentation strategies you have found most effective.

Contributions 2

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Mientras tanto dejo mi aporte por ac谩: Importante para los que usan WSL, se puede usar **ngrok** a continuaci贸n dejo un paso a paso pero antes quiero explicar lo que yo entend铆 del problema. Desde WSL no podemos exponer directamente el puerto (para mi caso us茅 4000), esto se hace desde Windows y la conexi贸n entre WSL y Windows para exponer dicho puerto la facilitar谩 **ngrok**: 1. Vayan a <https://ngrok.com/downloads/windows?tab=download> y descarguen el instalador de Windows, descompriman y corran el .exe. 2. Eso les abrir谩 la terminal en Windows. Deben configurar el authtoken de ngrok, para eso creense una cuenta en la p谩gina de ngrok y en el apartado "Your Authtoken" copien `ngrok config add-authtoken <AuthToken>` 3. Vayan a esa terminal ejecutenlo y luego ejecuten `ngrok http <puerto_WSL>` (que para mi caso fue 4000) de esa forma les entregar谩 la url desde la que se puede acceder, antes de pegarla en meta recuerden agregar /webhook a la URL y listo!
Tengo problemas porque instal茅 postman en Windows y todo el proyecto lo tengo en WSL, pude leer sobre el uso de **ngrok** para resolver el problema pero quisiera la soluci贸n recomendada por Platzi para los que trabajamos con esta configuraci贸n de entorno de desarrollo, nos podr铆an proporcionar una soluci贸n recomendada? Tuve problemas con el curso de whatsapp API y no pude avanzar y ac谩 voy por el mismo camino. Gracias