{"id":25268,"date":"2022-06-17T10:27:36","date_gmt":"2022-06-17T08:27:36","guid":{"rendered":"https:\/\/www.makingscience.com\/?p=25268"},"modified":"2022-06-17T10:27:36","modified_gmt":"2022-06-17T08:27:36","slug":"curl-que-es-y-para-que-sirve","status":"publish","type":"post","link":"https:\/\/www.makingscience.com\/es\/blog\/curl-que-es-y-para-que-sirve\/","title":{"rendered":"cURL, \u00bfqu\u00e9 es y para qu\u00e9 sirve?"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Seguramente que alguna vez te has encontrado en la tesitura de tener que conectar contra alg\u00fan servicio ya sea FTP, SFTP, HTTP o incluso LDAP y no ten\u00edas el cliente apropiado instalado.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Todas estas acciones las podemos realizar con cURL pero, <strong>\u00bfqu\u00e9 es cURL realmente y c\u00f3mo puede ayudarnos?<\/strong><\/span><\/p>\n<h3><strong>Qu\u00e9 es cURL<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">cURL es la abreviatura de \u201cClient URL\u201d y es una<strong> herramienta de l\u00ednea de comandos utilizada para la transferencia de ficheros con formato URL.<\/strong> Soporta multitud de protocolos entre los que se encuentran FTP, SFTP, SCP, LDAP y por supuesto HTTP y HTTPS, y est\u00e1 formado por dos partes; <strong>la librer\u00eda libcurl<\/strong> y <strong>el int\u00e9rprete curl.<\/strong> <\/span><\/p>\n<p><span style=\"font-weight: 400;\">En este art\u00edculo <strong>nos centraremos en el uso de cURL para los protocolos HTTP y HTTPS.<\/strong><\/span><\/p>\n<p><span style=\"font-weight: 400;\">Para poder seguir el blog de forma correcta primero debemos conocer qu\u00e9 es el protocolo HTTP y c\u00f3mo funcionan las cabeceras HTTP. Si a\u00fan no lo sabes, puedes echarle un vistazo a este <\/span><a href=\"https:\/\/www.makingscience.es\/blog\/que-es-http-y-como-funciona\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">post<\/span><\/a><span style=\"font-weight: 400;\"> que tenemos sobre ello.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Como hemos comentado anteriormente, cURL es una herramienta de l\u00ednea de comandos. Aunque cURL es utilizado por multitud de programas y est\u00e1 presente en la mayor\u00eda de SO primero tenemos que comprobar si cURL est\u00e1 instalado en nuestro sistema. A lo largo del post utilizamos Linux como sistema operativo de referencia para todas las pruebas, pero t\u00fa puedes instalar cURL en tu <\/span><a href=\"https:\/\/curl.se\/download.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">sistema operativo favorito<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Una vez tengamos instalado cURL, vamos a realizar nuestra primera petici\u00f3n HTTP. <\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">La sintaxis de cURL se forma de la siguiente manera:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Usage: curl [options&#8230;] &lt;url&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Por lo que para realizar una simple petici\u00f3n GET podemos ejecutar lo siguiente:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl <\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\"><strong>Seguramente ahora mismo ver\u00e1s tu terminal inundado de c\u00f3digo HTML y JavaScript \u00bfpor qu\u00e9 ocurre esto?<\/strong> Este c\u00f3digo es la respuesta que nos ha dado www.makingscience.com a nuestra petici\u00f3n. Es la p\u00e1gina completa que ver\u00edamos al visitar www.makingscience.com desde nuestro navegador.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cURL act\u00faa como un \u201cnavegador\u201d desde la terminal, por lo que todo el contenido del cuerpo de nuestra petici\u00f3n, va a ser mostrado por la terminal.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">En multitud de ocasiones el contenido del cuerpo de la respuesta no lo necesitamos ya que solo queremos comprobar el estado de nuestra petici\u00f3n o la informaci\u00f3n de las cabeceras. Para poder hacer una petici\u00f3n sin recibir todo el contenido podemos utilizar la opci\u00f3n \u201c-I\u201d en nuestra petici\u00f3n cURL. De este modo realizaremos una petici\u00f3n de tipo \u201cHEAD\u201d para recibir \u00fanicamente la informaci\u00f3n de las cabeceras.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -I <\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">HTTP\/2 200\u00a0 <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">server:<\/span><span style=\"font-weight: 400;\"> nginx<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">date:<\/span><span style=\"font-weight: 400;\"> Wed, 18 May 2022 14:24:25 GMT<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">content-<\/span><span style=\"font-weight: 400;\">type:<\/span><span style=\"font-weight: 400;\"> text\/html; charset=UTF-8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">vary:<\/span><span style=\"font-weight: 400;\"> Accept-Encoding<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">link:<\/span><span style=\"font-weight: 400;\"> &lt;<\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/wp-json\/&gt;; rel=\u00bbhttps:\/\/api.w.org\/\u00bb<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">link:<\/span><span style=\"font-weight: 400;\"> &lt;<\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/wp-json\/wp\/v2\/pages\/11001&gt;; rel=\u00bbalternate\u00bb; type=\u00bbapplication\/json\u00bb<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">link:<\/span><span style=\"font-weight: 400;\"> &lt;<\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/&gt;; rel=shortlink<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">access-control-allow-<\/span><span style=\"font-weight: 400;\">origin:<\/span><span style=\"font-weight: 400;\"> *<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2026<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">En esta ocasi\u00f3n podemos ver que s\u00f3lo nos est\u00e1 mostrando el c\u00f3digo de respuesta del servidor y las cabeceras que nos devuelve www.makingscience.com.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Ejemplos b\u00e1sicos de cURL<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Dado que ya tenemos una breve introducci\u00f3n de qu\u00e9 es y c\u00f3mo funciona cURL, vamos a ir explicando algunos ejemplos b\u00e1sicos y de gran utilidad para los que podamos usar cURL.<\/span><\/p>\n<ul>\n<li><strong>Guardar salida a fichero<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Podemos utilizar las opciones -o y -O para guardar la respuesta en un fichero \u00bfcu\u00e1l es la diferencia entre -o y -O? Con la o min\u00fascula podemos especificar el fichero espec\u00edfico donde queramos guardar la respuesta y con la O may\u00fascula nos guardar\u00e1 el fichero con el nombre que tiene el recurso pedido.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -o index.html https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -O https:\/\/www.makingscience.com\/wp-content\/themes\/fundaciontheme\/fonts\/graphik\/ttf\/Graphik-Regular.ttf<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">En el primer caso, nos guardar\u00e1 el recurso pedido como index.html. En el segundo ejemplo, nos guardar\u00e1 el fichero Graphik-Regular.ttf con el mismo nombre.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Esta opci\u00f3n tambi\u00e9n nos sirve para enviar la respuesta que no queramos a cualquier sitio, por ejemplo, a \/dev\/null.<\/span><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>Seguir redirecciones<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Con la opci\u00f3n \u2013location o -L podemos seguir las redirecciones que haga nuestra petici\u00f3n. Si realizamos una petici\u00f3n y el c\u00f3digo de respuesta es un 30X, esto quiere decir que no es el destino final de nuestra petici\u00f3n, si no que el servidor nos est\u00e1 redirigiendo a otro sitio. cURL por defecto no sigue esa redirecci\u00f3n, si no que nos devuelve ese c\u00f3digo y finaliza la ejecuci\u00f3n.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -I <\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/makingscience.com\/\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">HTTP\/2 301\u00a0 <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">cache-<\/span><span style=\"font-weight: 400;\">control:<\/span> <b>private<\/b><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">content-<\/span><span style=\"font-weight: 400;\">type:<\/span><span style=\"font-weight: 400;\"> text\/html; charset=UTF-8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">referrer-<\/span><span style=\"font-weight: 400;\">policy:<\/span><span style=\"font-weight: 400;\"> no-referrer<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">location:<\/span> <span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2026<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Como vemos en el ejemplo anterior, el servidor devuelve un 301 ya que realiza una petici\u00f3n a la URL con www. En el caso de hacer la misma petici\u00f3n pero con la opci\u00f3n -L, podemos ver que cURL sigue la redirecci\u00f3n y nos muestra el resultado final con las dos respuesta.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -IL <\/span><a href=\"https:\/\/makingscience.com\/\"><span style=\"font-weight: 400;\">https:\/\/makingscience.com\/<\/span><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">HTTP\/2 301\u00a0 <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">cache-<\/span><span style=\"font-weight: 400;\">control:<\/span> <b>private<\/b><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">content-<\/span><span style=\"font-weight: 400;\">type:<\/span><span style=\"font-weight: 400;\"> text\/html; charset=UTF-8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">referrer-<\/span><span style=\"font-weight: 400;\">policy:<\/span><span style=\"font-weight: 400;\"> no-referrer<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">location:<\/span> <span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2026<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">HTTP\/2 200\u00a0 <\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">server:<\/span><span style=\"font-weight: 400;\"> nginx<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">date:<\/span><span style=\"font-weight: 400;\"> Wed, 18 May 2022 14:34:43 GMT<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u2026<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>Uso de otros m\u00e9todos HTTP<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Al principio del post, explicamos c\u00f3mo realizar peticiones para ver s\u00f3lo las cabeceras de la petici\u00f3n, esta opci\u00f3n es posible ya que cURL realiza una petici\u00f3n de tipo HEAD en lugar de un GET cuando utilizamos el par\u00e1metro -I pero \u00bfc\u00f3mo podemos hacer peticiones con otros m\u00e9todos HTTP? Para hacer peticiones con otros m\u00e9todos, podemos utilizar la opci\u00f3n -X seguido del m\u00e9todo que queramos.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl -X OPTIONS <\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">curl -X POST <\/span><span style=\"font-weight: 400;\">https:<\/span><span style=\"font-weight: 400;\">\/\/www.makingscience.com\/<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>A\u00f1adir cabeceras HTTP<\/strong><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Acabamos de aprender c\u00f3mo cambiar el m\u00e9todo de nuestra petici\u00f3n en cURL. Muchas veces vamos a querer a\u00f1adir cabeceras extra, por ejemplo, si queremos hacer una subida a trav\u00e9s de POST a un API. Para a\u00f1adir cabeceras con cURL tenemos que indicarle la opci\u00f3n -H seguido de la cabecera que queramos establecer:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl<\/span><span style=\"font-weight: 400;\"> -X POST -H <\/span><span style=\"font-weight: 400;\">\u00abContent-Type: application\/json\u00bb<\/span> <span style=\"font-weight: 400;\">\u00abhttps:\/\/www.example.com\u00bb<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Podemos incluir m\u00e1s de una cabecera a\u00f1adiendo m\u00e1s opciones con -H<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">curl<\/span><span style=\"font-weight: 400;\"> -X POST -H <\/span><span style=\"font-weight: 400;\">\u00abContent-Type: application\/json\u00bb<\/span><span style=\"font-weight: 400;\"> -H <\/span><span style=\"font-weight: 400;\">\u00abAccept: application\/json\u00bb<\/span> <span style=\"font-weight: 400;\">\u00abhttps:\/\/www.example.com\u00bb<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3><strong>Conclusi\u00f3n<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Aunque <strong>cURL<\/strong> no es la herramienta m\u00e1s intuitiva para realizar peticiones HTTP como podr\u00eda ser Postman, Insomnia o alguna otra con GUI, si que <strong>nos permite una flexibilidad y configuraci\u00f3n mayor.<\/strong> Podemos ejecutar cURL pr\u00e1cticamente desde cualquier parte y adaptar la herramientas a nuestros scripts, recuerda que <strong>cURL no solo soporta HTTP si no que la lista de <\/strong><\/span><strong><a href=\"https:\/\/curl.se\/docs\/manpage.html\" target=\"_blank\" rel=\"noopener\">protocolos soportados<\/a> es mucho m\u00e1s amplia.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seguramente que alguna vez te has encontrado en la tesitura de tener que conectar contra alg\u00fan servicio ya sea FTP, SFTP, HTTP o incluso LDAP y no ten\u00edas el cliente apropiado instalado. Todas estas acciones las podemos realizar con cURL pero, \u00bfqu\u00e9 es cURL realmente y c\u00f3mo puede ayudarnos? Qu\u00e9 es cURL cURL es la [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":25285,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[315,125],"class_list":["post-25268","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-ai","tag-automation","tag-systems-2"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/posts\/25268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/comments?post=25268"}],"version-history":[{"count":0,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/posts\/25268\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/media\/25285"}],"wp:attachment":[{"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/media?parent=25268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/categories?post=25268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makingscience.com\/es\/wp-json\/wp\/v2\/tags?post=25268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}