HTB academy — Web requests :
HTTP Methods — CURD API
En esta entrada, veremos cómo interactuar con una API CRUD (Create, Read, Update, Delete) utilizando métodos HTTP, siguiendo la guía de HTB Academy. Mediante ejemplos prácticos, aprenderemos a realizar operaciones básicas como lectura, creación, actualización y eliminación de datos en la API. Para esto, emplearemos la herramienta curl
y utilizaremos jq
para formatear las respuestas en JSON.
Leer -read
Para realizar una consulta simple a la API y obtener información de una ciudad específica, como Londres, podemos usar el siguiente comando:
zhunter12@htb[/htb]$ curl http://<SERVER_IP>:<PORT>/api.php/city/london
[{"city_name":"London","country_name":"(UK)"}]
Para formatear la respuesta correctamente en JSON, utilizamos jq
:
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/london | jq
[
{
"city_name": "London",
"country_name": "(UK)"
}
]
Si se pasa un string vacío, obtendremos todas las opciones disponibles:
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/ | jq
[
{
"city_name": "London",
"country_name": "(UK)"
},
{
"city_name": "Birmingham",
"country_name": "(UK)"
},
{
"city_name": "Leeds",
"country_name": "(UK)"
},
...otros registros...
]
Crear — Create
Para agregar una nueva ciudad a la API, utilizamos el método POST:
zhunter12@htb[/htb]$ curl -X POST http://<SERVER_IP>:<PORT>/api.php/city/
-d '{"city_name":"HTB_City", "country_name":"HTB"}'
-H 'Content-Type: application/json'
Podemos verificar la ciudad creada:
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/HTB_City | jq
[
{
"city_name": "HTB_City",
"country_name": "HTB"
}
]
Actualizar — Update
Para actualizar los detalles de una ciudad existente, usamos el método PUT (también se puede utilizar PATCH, pero en este lab usaremos PUT):
zhunter12@htb[/htb]$ curl -X PUT http://<SERVER_IP>:<PORT>/api.php/city/london
-d '{"city_name":"New_HTB_City", "country_name":"HTB"}'
-H 'Content-Type: application/json'
Hemos reemplazado el valor de Londres por New_HTB_City. Ahora, no debería existir el registro para Londres:
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/london | jq
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/New_HTB_City | jq
[
{
"city_name": "New_HTB_City",
"country_name": "HTB"
}
]
Eliminar — Delete
Para eliminar una ciudad de la API, usamos el método DELETE:
zhunter12@htb[/htb]$ curl -X DELETE http://<SERVER_IP>:<PORT>/api.php/city/New_HTB_City
zhunter12@htb[/htb]$ curl -s http://<SERVER_IP>:<PORT>/api.php/city/New_HTB_City | jq
[]
Lab CURD API:
Questions
Answer the question(s) below to complete this Section and earn cubes!
try to update any city's name to be 'flag'. Then, delete any city.
Once done, search for a city named 'flag' to get the flag.
Para completar el lab, debemos seguir estos pasos:
- Consultar todas las ciudades y formatear la salida en JSON:
curl -s <http://94.237.49.212:53460/api.php/city/> | jq
[
{
"city_name": "London",
"country_name": "(UK)"
},
{
"city_name": "Glasgow",
"country_name": "(UK)"
},
{
"city_name": "Baltimore",
"country_name": "(US)"
}
...otros registros...
]
2. Actualizar el nombre de una ciudad a ‘flag’:
curl -X PUT <http://94.237.49.212:53460/api.php/city/Boston> -d '{"city_name":"flag", "country_name":"HTB"}' -H 'Content-Type: application/json'
3. Eliminar cualquier ciudad, por ejemplo, Londres:
curl -X DELETE <http://94.237.49.212:53460/api.php/city/London>
4. Buscar la ciudad llamada ‘flag’:
curl -s <http://94.237.49.212:53460/api.php/city/flag> | jq
[
{
"city_name": "flag",
"country_name": "HTB{crud_4p!_m4n!pul4t0r}"
}
]
5. flag = HTB{crud_4p!_m4n!pul4t0r}