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

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
13 Hrs
15 Min
9 Seg

Explorando la API

2/25
Resources
Transcript

How to explore our API using Insomnia or Postman?

Exploring and testing an API can be a daunting task, but with the right tools, such as Insomnia or Postman, it is much easier than it seems. Insomnia is a software that allows us to make requests in an organized and structured way to our API. You can also use Postman, and both will offer you an effective way to interact with the API. In the project repository, you will find files like insomnia.json or postman.json to import the whole API and quickly configure it in the tool of your choice.

How to configure a request environment?

  1. Import configuration file:

    • For Insomnia: Import the insomnia.json file.
    • For Postman: Use the postman.json file.
  2. Configure local environment:

    • Verify that the application is running on localhost port 3000.
    • Make a request to verify that you can receive the "HelloMyServer on express" response.
  3. Subdirectories and endpoints:

    • The API is structured in subdirectories such as categories, notification, customers, orders, products, profile and users.
    • Each subdirectory contains specific endpoints for your services, such as getting all categories, creating new ones, etc.

How to create a product in the API?

Before creating a product, it is vital that the database and migrations are configured correctly, as the API makes use of a migration system that structures the necessary tables.

  1. Raise the database:

    • Stop the development server.
    • Lift the database with Docker:
      docker-compose up -d postgres
    • Verify its execution with:
      docker-compose ps
      .
  2. Run the migrations:

    • Run the command for migrations:
      npm run migrations:run
    • Restart the development server:
      npm run dev
  3. Create a category:

    • Make a request to create a category, providing the name and image.

How to authenticate and create data?

The API has authentication protections, which means that some actions require you to be logged in as an administrator.

  1. Create an administrator user:

    • Access the endpoint users/create and create a user with email and password.
  2. Login:

    • Login to the auth/login endpoint using the email and password to obtain an access token.
  3. Use access token:

    • Specify the access token in the environment variables in Insomnia or Postman.
    • Insert it in the authentication section as Bearer Token.

How to handle protected endpoints?

There are endpoints in the API that are protected, such as creating categories, which requires authentication. On the other hand, there are some, such as creating products, that do not require verification. It is relevant to distinguish these cases and prepare an appropriate testing scheme.

  1. Test protections:

    • Try to create a category without authentication and receive the 401 error response indicating that it is an unauthorized action.
  2. Configure and validate users:

    • Create a user and make sure they are registered in the database.
    • Use their authentication to perform administrative actions.

How to practice and get familiar with the API?

Invest time in interacting with the API by creating categories and products. For example:

  • Create at least three categories with your admin user.
  • Develop ten products distributed among those categories.

These activities will provide you with relevant data in the database to perform experiments and tests with the API. This kind of practice will help you learn more about the inner workings and structuring of the API.

Don't stop here! Keep exploring and expand your skills with the tool you feel most comfortable with, and you will soon see the fruits of your efforts in professional development.

Contributions 8

Questions 5

Sort by:

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

Hola, cuando corro la migraci贸n me salta el siguiente c贸digo de error:

Loaded configuration file "src/db/config.js".
Using environment "development".

ERROR: password authentication failed for user "nico"

Esto me pasa con cualquier usuario y no es la primera vez. 驴C贸mo puedo solucionarlo?

ese postman.json no est谩 馃槮

el archivo insomnia.json no funciona

Hay una forma que utilizo para guardar el pm.environment.set("ACCESS\_TOKEN", pm.response.json().access\_token);`ACCESS_TOKEN` en postman, en las pesta帽as aparece una parte que se llama test, vamos a darle click y colocaremos lo siguiente ```js pm.environment.set("ACCESS_TOKEN", pm.response.json().access_token); ```

En un curso anterior de docker me di贸 un error que dec铆a que no se pod铆a acceder a la carpeta postgres_data o que esta carec铆a de permisos, si les pasa lo mismo pueden hacer esto:

# Creamos un volumen local
$ docker volume create --name postgres -d local

# El c贸digo es identico al de la clase salvo por la parte de volumes de postgres
version: '3.3'

services:
  postgres:
    image: postgres:13
    environment:
      - POSTGRES_DB=my_store
      - POSTGRES_USER=nico
      - POSTGRES_PASSWORD=admin123
    ports:
      - 5432:5432
    volumes:
      - postgres:/var/lib/postgresql/data
# Ac谩 en volumes es diferente, no se olviden de cambiarlo, lo otro es igual
  pgadmin:
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@mail.com
      - PGADMIN_DEFAULT_PASSWORD=root
    ports:
      - 5050:80
# Este agregado es para que funcione el volumen local, no se olviden de borrar esta los comentarios si copian y pegan para evitar errores
volumes:
    postgres:
      external: true
Lo que pueden hacer para solucionar el problema con el user nico es lo siguiente: 1\. Eliminar el contenedor de docker que se crea. 1. Iniciar una terminal en la ruta donde tengan el curso y ejecutar `psql postgres` 2. ejecutar `CREATE USER nico WITH PASSWORD 'admin123';` 3. ejecutar `CREATE DATABASE my_store OWNER nico;` Eso deber铆a solucionar el problema.
hola, me sale este error y no s茅 como arreglarlo: `"message": "role \"nico\" does not exist",` adem谩s si cambio nico por otro rol tambi茅n falla.

para quienes no sepan como arrancar, recomiendo leer la doc de insomnia
https://docs.insomnia.rest/insomnia/import-export-data