Linux отправить get запрос

How to send HTTP request using curl and wget command from Linux and UNIX? Example Tutorial

You can use either curl or wget command to send HTTP requests from UNIX or Linux operating system. Both commands allow you to send GET and POST requests, which means you can also call REST web services. I have a Java web application, which runs on Linux and exposes WebServices. I was writing a UNIX script to download In some data from that web service when I hit by the question, how do I make an HTTP call from UNIX? What is the UNIX command should I use? If you are also facing the same problem, then you have come to the right ht place. Basically, you can use two UNIX commands to make the HTTP request, wget , and curl .

You can use curl and wget to send both GET and POST HTTP requests. My use case was rather simple, I just need to make a GET call to web service to download the data, but these two commands are mighty and provide many different options to interact with the web using HTTP right from the shell.

Between wget and curl (also known as cURL), curl (I call it «karl») is my favorite. Why? I think because I have used it more often than wget :-).

You can make your choices if you have good knowledge of the Linux command line, and that’s why I suggest every programmer and software developer join Learn Linux in 5 Days and Level Up Your Career course on Udemy, the best way to become efficient in Linux.

Now let’s see how to send HTTP requests from UNIX using the curl command.

Linux Command to send HTTP GET request

Here is one example of calling web service from Linux shell by sending HTTP GET request using cURL command:

$ curl http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=bd82977b86bf27fb59a04b61b657fb6f "coord":"lon":-0.13,"lat":51.51>,"weather":[ "id":804,"main":"Clouds","description":"overcast clouds","icon":"04n">], "base":"stations", "main":"temp":282,"pressure":1022,"humidity":87,"temp_min":277.15,"temp_max":285.15>, "visibility":10000,"wind":"speed":1.5>,"clouds":"all":90>,"dt":1445577409, "sys":"type":1,"id":5093,"message":0.0201,"country":"GB", "sunrise":1445582275,"sunset":1445619056>,"id":2643743,"name":"London","cod":200>

You can also specify timeout using — m option as shown below:

$ curl -m 2 http://api.openweathermap.org/data/2.5/weather ?q=London,uk&appid=bd82977b86bf27fb59a04b61b657fb6f

This request will timeout in 2 seconds if it doesn’t receive any response.

Читайте также:  Pantum 6500 driver linux

You can also use wget to send HTTP requests and download the data. The only difference between curl and wget is that curl will print output in console and wget will store it in the file e.g.

$ wget http://localhost:8080/index.html

will download the content of index.html and store it into a file with the same name.

Here is how you can use curl to download wget command in UNIX:

How to send HTTP request from UNIX? Use CURL command

UNIX command to Send an HTTP POST request

You can also use curl to send HTTP post requests. All you need to do is use the —data option to specify the data you want to POST to web service e.g.

$ curl --data "param1=value1¶m2=value2" http://locahost:8080/weather

if you want to send data from file to web service, you can also use the following command:

$ curl -X POST -d @filename http://locahost:8080/weather

Similarly, if you’re going to upload a file, you can do so by executing the following command:

$ curl --form "fileupload=@filename.txt" http://locahost:8080/weather

Btw, these are just the tip of the iceberg when it comes to seeing the real power of curl command. If you want to learn more, I suggest you check out the Linux Command Line Basics course on Udemy. One of the best courses to learn basic and advanced Linux commands.

best course to learn Linux commands for beginners

That’s all about how to send HTTP requests from UNIX and Linux. It’s simple, just remember the curl and wget command. You can explore their option by using man wget and man curl. Also, remember the difference between curl and wget, the former prints the output in the console while the later store the response in the same file as requested. I personally like curl because it’s easier to use but if you like wget stick with it.

  • 10 examples of find command in UNIX (examples)
  • 10 examples of grep command in UNIX (examples)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of xargs command in Linux (examples)
  • 10 Courses to learn Linux commands for beginners (best courses)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • How to delete empty files and directory in UNIX (solution)
  • How to make a directory tree in one command? (example)
  • How to how long argument of a process in Solaris (command)
  • UNIX command to find out how long a process is running? (answer)
  • UNIX command to find the size of the file and directory? (command)
  • 5 examples of sort command in Linux (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 examples of chmod command in UNIX (examples)
  • 10 tips for working fast in UNIX? (tips)
Читайте также:  Thin client linux install

P. S. — If you want to learn Linux and looking for some free resources like books and online courses, then you can also check out this list of free Linux courses for Programmers and IT Professionals. This list contains some of the best free courses from Udemy, Pluralsight, Coursera, Codecademy, and other online platforms.

Источник

Использование Curl для выполнения запросов REST API

Интерфейс прикладных программ (API) — это набор определений и протоколов, которые позволяют программам взаимодействовать друг с другом.

Термин REST означает передачу репрезентативного состояния. Это архитектурный стиль, состоящий из набора ограничений, используемых при создании веб-сервисов.

RESTful API — это API, который следует архитектуре REST. Обычно API-интерфейсы REST используют протокол HTTP для отправки и получения данных и ответов в формате JSON. Вы можете использовать стандартные методы HTTP для создания, просмотра, обновления или удаления ресурсов через API.

Для тестирования API RESTful и взаимодействия с ними вы можете использовать любую библиотеку или инструмент, который может выполнять HTTP-запросы.

Запросы API состоят из четырех разных частей:

  • Конечная точка. Это URL-адрес, который клиент использует для связи с сервером.
  • Метод HTTP. Он сообщает серверу, какое действие хочет выполнить клиент. Наиболее распространенные методы — GET POST PUT DELETE и PATCH
  • Заголовки. Используется для передачи дополнительной информации между сервером и клиентом, например авторизации.
  • Тело. Данные отправлены на сервер.

В этой статье мы собираемся обсудить, как использовать curl для взаимодействия с RESTful API. curl — это утилита командной строки для передачи данных с или на удаленный сервер. Он установлен по умолчанию в macOS и большинстве дистрибутивов Linux.

Параметры завивки

Синтаксис команды curl следующий:

Читайте также:  Linux on acer aspire one d255

Вот параметры, которые мы будем использовать при выполнении запросов:

  • -X , —request — HTTP-метод, который будет использоваться.
  • -i , —include — включить заголовки ответа.
  • -d , —data — данные для отправки.
  • -H , —header — дополнительный заголовок для отправки.

HTTP GET

Метод GET запрашивает у сервера определенный ресурс.

GET — это метод по умолчанию при выполнении HTTP-запросов с помощью curl . Вот пример выполнения запроса GET к API JSONPlaceholder для представления JSON всех сообщений:

curl https://jsonplaceholder.typicode.com/posts

Для фильтрации результатов используйте параметры запроса:

curl https://jsonplaceholder.typicode.com/posts?userId=1

HTTP POST

Метод POST используется для создания ресурса на сервере. Если ресурс существует, он перезаписывается.

Следующая команда выполняет запрос POST, используя данные, указанные с параметром -d :

curl -X POST -d "userId=5&title=Hello World&body=Post body." https://jsonplaceholder.typicode.com/posts

Тип тела запроса указывается с помощью заголовка Content-Type . По умолчанию, когда этот заголовок не задан, curl использует Content-Type: application/x-www-form-urlencoded .

Чтобы отправить данные в формате JSON, установите тип тела в application/json :

curl -X POST -H "Content-Type: application/json" -d '' https://jsonplaceholder.typicode.com/posts

HTTP PUT

Метод PUT используется для обновления или замены ресурса на сервере. Он заменяет все данные указанного ресурса данными запроса.

curl -X PUT -d "userId=5&title=Hello World&body=Post body." https://jsonplaceholder.typicode.com/posts/5

HTTP-ПАТЧ

Метод PUT используется для частичного обновления ресурса на сервере.

curl -X PUT -d "title=Hello Universe" https://jsonplaceholder.typicode.com/posts/5

HTTP УДАЛИТЬ

Метод DELETE удаляет указанный ресурс с сервера.

curl -X DELETE https://jsonplaceholder.typicode.com/posts/5

Аутентификация

Если конечная точка API требует аутентификации, вам необходимо получить ключ доступа. В противном случае сервер API ответит ответным сообщением «Доступ запрещен» или «Неавторизован».

Процесс получения ключа доступа зависит от API, который вы используете. Получив токен доступа, вы можете отправить его в заголовке:

curl -X GET -H "Authorization: Bearer " "https://api.server.io/posts"

Выводы

Мы показали вам, как использовать curl для выполнения тестовых запросов API. Дополнительные сведения о curl см. На странице документации по Curl .

Если у вас есть какие-либо вопросы или отзывы, не стесняйтесь оставлять комментарии.

Источник

Оцените статью
Adblock
detector