Curl in windows powershell

Adam the Automator

Curl vs. PowerShell: Comparing Use Cases

Devin Rich

Read more posts by this author.

Curl is a versatile tool that allows you to make HTTP calls across a wide range of situations. It’s been around mostly in the Linux world for many years but more recently in Windows 10. But the scripting language PowerShell can accomplish similar tasks that curl can. Is PowerShell a good alternative to curl? What are the differences? Let’s find out!

In this article, let’s compare PowerShell and curl side-by-side to discover which one works best in various contexts. We’ll also compare each in various use cases for you to make the best decision based on your personal situation.

Table of Contents

Myth Busting: PowerShell Cmdlets <> Curl

Before we get too deep into this article, let’s bust a popular myth that PowerShell’s Invoke-WebRequest or Invoke-RestMethod cmdlets are “replacements” for curl. This simply isn’t true. By looking at the help content for these commands, it’s clear each has different capabilities and use cases.

Take a look at what the curl documentation says:

curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP).

The command is designed to work without user interaction. Curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more.

Now compare that to the Invoke-WebRequest and Invoke-RestMethod cmdlet help documentation.

The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements.”

The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based on the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes. For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into objects.”

Breaking down how each tool is described, curl aims to be the premiere solution for transferring data from or to a server over many protocols.

Contrast that with the Invoke-WebRequest and Invoke-RestMethod PowerShell cmdlets that focus on more common communications with servers. These PowerShell cmdlets also provide a rich experience for working with returned data through PowerShell’s use of objects.

Long Parameters in Bash vs. PowerShell

Since curl isn’t a scripting language like PowerShell is, it relies on the command shell it’s executed from. Most of the time that command shell is Bash.

Both the PowerShell cmdlets and curl both may require different parameters that may extend far past a terminal’s width. To make the syntax easier to read, each shell scripting language has its own way of handling this.

Take the below code for example. This example demonstrates sending an HTTP POST verb to a URL using a small bit of data. This code could be placed on one line but it would soon begin to wrap the screen or require scrolling. To remediate this, in Bash, you can use backslashes to continue the command onto a new line.

Contrast the line-wrapping method with PowerShell using either cmdlet. By using splatting, you can structure the same request performing the same task.

You can also mix and match the splatted and named parameters as shown below.

Working with REST APIs

REST APIs are a common feature tied to many products. Since querying REST APIs is a common use of both curl and the PowerShell cmdlets, let’s first compare and contrast the various ways these two tools handle them.

Note that the tasks you’ll be learning will work just as well with standard HTTP URIs. Both curl and the PowerShell cmdlets will work with any HTTP endpoint.

Getting Data From a REST API

First up, the easiest task to demonstrate is getting a response from a REST API. Both curl and the PowerShell cmdlets can do this with a single URI parameter as shown below. You can see below that each immediately returns the JSON response’s body.

Читайте также:  Parallels desktop восстановит windows

The Invoke-RestMethod cmdlet, unlike Invoke-WebRequest , automatically converts response data from JSON to PowerShell objects. But you can do the same with Invoke-WebRequest by using the ConvertFrom-Json cmdlet as shown below.

Submitting Data to a REST API

Curl and PowerShell cmdlets can also submit data to a URI using the POST verb. When submitting data via POST, you’ll usually include a body as well which both tools are capable of.

You can see below using the —request parameter for curl and the Method parameter with Invoke-RestMethod , you can specify the verb. For the body, you’ll use the —data parameter in curl and the Body parameter with Invoke-RestMethod .

By default, curl and Invoke-RestMethod will send POST requests using a content type of application/x-www-form-urlencoded.

If the API only allows a specific content type or you’ll need to manually specify a one via a parameter. Below you can see that curl uses the more generic header parameter while the Invoke-RestMethod cmdlet allows you to specify the content type separately using the ContentType parameter.

You can also use curl’s shortcut -H parameter instead of —header .

Using Custom Headers

In the above example, you saw how you could pass the content-type header to a URI using both curl and PowerShell. You can also add more at the same time if you’d wish.

To add more headers in curl, you’ll simply add another —header or -H providing the key and value of the header you’d like to send as shown below.

You can do the same in PowerShell also as shown below. By passing a hashtable with headers’ key/value pairs to the Headers parameter, you can send as many headers as you’d like.

Parsing the HTTP Response

Now that you have seen how to get simple data, let’s extract some useful information from it.

One big advantage of using PowerShell vs. curl is the native ability to parse the response. Since curl is a utility and not a scripting language, you’ll typically need to use another utility to parse the response. To level the playing field, you’ll see many people parsing curl’s response using Python or Perl or using a tool called jq. Jq is a utility that allows you to parse JSON data.

To demonstrate parsing, using the previous example, perhaps you’d like to extract only the title from the response. The example below uses curl to explicitly specify to use the GET verb using the —request parameter (curl defaults all requests to GET). The example is also using the silent -s parameter to avoid some on-screen statistics.

Since you learned that the main difference between the Invoke-WebRequest cmdlet and the Invoke-RestMethod cmdlet is Invoke-RestMethod ‘s native ability to parse the response, let’s skip Invoke-WebRequest in this example.

Below you will see, as with curl, explicitly specifying the GET method changes nothing. Also notice that the command and parameter is enclosed in parentheses. This allows you to then reference the object property Title using dot notation.

Scraping a Webpage

When you pull data from a webpage, you will probably want to do additional processing on the output. In Bash, that may mean using an HTML parser like hxselect provided by the package html-xml-utils . In contrast, PowerShell’s Invoke-WebRequest cmdlet can parse much of what you want automatically.

To demonstrate, let’s extract all HTML references from www.google.com.

In Bash, you’ll need the html-xml-utils package installed. To query a webpage and parse it with hxselect , you have to normalize the web response to xhtml ( -x ) format. You’ll also need to use a text processing tool, like Grep to filter and select the output.

The example command below is using Grep’s Regex matching ( -P ) and only-matched ( -o ) to split out the results onto their own lines:

Читайте также:  Феникс майнер настройка windows 10

In PowerShell, you’ll only need to specify that you want the href property from the links in these tags.

Submitting a Form

Forms are used for many things, such as login, search, uploads, etc. While PowerShell can easily select forms from a webpage and provide a hashtable to fill out, using Curl + Bash requires more work.

In PowerShell, you must first gather a form, fill it out, then submit it. The example below is calling $Form.fields to view all of the available fields of the form. $Result is then populated with the response and you can inspect the google results through $Result.links.

Using Curl, you have to work to find the fields and then specify them yourself. The output of the first command shows me that there is a q form field I can use for search queries. I then specify data to be filled for that field when submitting the request:

Using a Web Session / Cookies

When you need to save cookies between commands, such as a website login to access private pages, you’ll also want your cookies saved between sessions.

Curl asks you to use a “cookie jar” to hold these cookies which you can then use as your input “cookies” once logged in. You’ll have to do some work to find the actual form submission data that you need.

Using curl, you can specify a username and password to authenticate and a place for the curl “cookie jar”. Then, using the cookie obtained from that call, specify the “cookie jar” in the subsequent call as shown below.

PowerShell also allows you to store and use cookies/sessions as well using the Invoke-WebRequest cmdlet’s SessionVariable parameter.

The SessionVariable will store the cookie returned. Once complete, you can then use the WebSession parameter to pass that cookie to subsequent requests. Below you can see an example of filling out a form and using the same cookie returned when the form was queried earlier.

Summary

PowerShell’s web cmdlets Invoke-WebRequest and Invoke-RestMethod are useful web querying and parsing cmdlets. They hold some similarity to curl, but, as you’ve seen, both tools are different in their approach.

Since the cmdlets are part of the PowerShell scripting language, they are built with PowerShell in mind. The PowerShell approach differs from the curl utility in that regard because curl has to rely on other languages and tools to perform the same tasks.

For a handy PowerShell module that will convert many curl commands to PowerShell format, please check out Curl2PS.

PowerShell equivalent of curl

Is there an equivalent of curl in PowerShell? Does it have some similar built-in capability or is there a 3 rd party cmdlet?

9 Answers 9

PowerShell 3.0 has the new command Invoke-RestMethod :

As of Powershell 5.0, if not before, curl is an alias for Invoke-WebRequest .

To use the unaliased command .

So return several properties of the request as follows .

. or just the content .

The equivalent aliased commands are .

Leveraging Powershell defaults and other aliases you could shorten the commands to

. but I wouldn’t recommend it. Verbose commands help others when reading your code.

Powershell 6.x

Use of Aliases discouraged

As of Powershell 6.x «Core» curl is no longer an alias for Invoke-WebRequest (the alias wget is also removed) . Instead use Invoke-WebRequest directly.

Curl is no longer an alias for Invoke-WebRequest (tested on Powershell 6.2.3), despite an apparent rejection of a motion in an RFC «to to remove the aliases curl and wget from Windows PowerShell».

That RFC notes «The wget/curl aliases were already removed from PowerShell Core so the problem [of having those aliases] was limited to Windows PowerShell.»

In the conclusion the Powershell team also encourages users «to not rely on aliases in scripts».

As @v6ak has noted in the comments using curl and wget in PowerShell (5.0 or lower) can be a problem in: unintentionally invoking the real curl or wget if installed side-by-side; and, in any case, causes confusion.

New Encoding

It is recommended you upgrade Powershell «core» (6.x or above) in order to take advantage of the default encoding utf8NoBOM , when using Invoke-WebRequest (and many other text outputting commands). If one was doing this explicitly you could do something like:

Читайте также:  Activating windows 64 bit

However, even when using a shorter, implicit, command .

. encoding with utf8NoBOM will be done (you can verify this, for example, by opening the saved file in Visual Studio Code and observing «UTF-8» in the status bar).

Files saved with utf8NoBOM tend to cause fewer problems when traveling through various ecosystems. Of course, if you need some other encoding you can set some alternative explicitly.

In Powershell 5.0 and lower the utf8NoBOM encoding was not available, let alone the default.

Как использовать команду cURL с PowerShell и скачивать файлы

Каждая операционная система имеет свой тип команд для выполнения задач администрирования, поддержки или управления или ежедневного использования. Некоторые из этих команд могут быть совместимы между всеми системами, но большинство — нет. Одним из примеров этого является cURL, который мы можем часто использовать в средах macOS, UNIX или Linux, но не в Windows, говоря о версиях PowerShell до 5.

cURL — это команда, которая часто используется через командные строки или сценарии для передачи данных на компьютер. Но даже помимо этого, cURL используется в других элементах, таких как автомобили, телевизоры, маршрутизаторы или принтеры, для выполнения передачи данных из Интернета.

cURL — это бесплатное программное обеспечение с открытым исходным кодом, и его объем настолько широк, что он поддерживает протоколы и файлы, такие как: DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet и TFTP, также поддерживает сертификаты SSL, HTTP POST, HTTP PUT, загрузка по FTP, загрузка по HTTP, прокси, HTTP / 2, куки, аутентификация Имя пользователя + пароль (Basic, Normal, Summary, CRAM-MD5, NTLM, Negotiate и Kerberos), файл передачи учебного плана, туннелирование прокси и многое другое.

Чтобы использовать cURL в Windows PowerShell, в данном случае в Windows 10, мы должны проверить версию PowerShell, которая должна быть не менее 5, чтобы убедиться в этом, мы выполним следующее:

Используя cURL в Windows PowerShell, мы расширим функциональность этой современной консоли управления Microsoft.

1. Как использовать cURL в Windows PowerShell

Шаг 1
Для этого случая мы можем выполнить следующее:

Мы видим, что эта ошибка генерируется, но в качестве псевдонима используется cURL. Псевдонимы в Windows PowerShell дают нам возможность создавать ярлыки для более обширных команд, чтобы не вводить весь синтаксис.

Шаг 2
Мы можем использовать командлет Get-Alias, и мы увидим следующее. Отметим, что на линии команда cURL отвечает за вызов команды Invoke-WebRequest.

Шаг 3
Так что для функций cURL идеальным способом, просто отредактируйте синтаксис, который мы выполнили, и в этом случае потребуется только выполнить следующее:

Шаг 4
Таким образом, таким образом, мы можем использовать cURL в Windows PowerShell. Мы можем видеть, что файл был загружен по указанному пути, в данном случае System32, потому что PowerShell был запущен от имени администратора, хуже, мы можем выбрать нужный путь:

2. Как удалить псевдоним Windows

Шаг 1
В случае, если у нас нет профиля в Windows PowerShell, первым шагом будет создание его с помощью следующей команды:

Шаг 2
Теперь мы откроем этот профиль в текстовом редакторе, выполнив следующее: В развернутый файл мы вставим следующее. Там мы переходим к сохранению изменений и закрываем записную книжку.

Затем мы должны выполнить следующую команду, чтобы перезагрузить профиль, или закрыть и снова открыть Windows Powershell, чтобы обновить изменения.

3. Альтернативы для установки cURL на Windows

Шаг 1
Практическим способом убедиться, что cURL установлен в нашей системе, является переход по следующей ссылке и загрузка там:

cURL для Windows

Другой способ установить cURL — использовать Chocolatey, который можно подробно проанализировать по следующей ссылке:

Chocolatey — Linux-подобный менеджер пакетов командной строки для систем Windows. Мы откроем PowerShell от имени администратора и выполним следующую команду:

Шаг 2
После того, как мы установили Chocolatey, мы можем использовать его для установки cURL, и мы должны открыть и закрыть PowerShell перед этим, чтобы применить изменения.

Мы выполним следующее, чтобы установить cURL. Там мы можем запустить скрипт или нет.

Шаг 3
Когда процесс закончится, мы увидим следующее:

Ниже мы увидим несколько полезных советов о том, как использовать cURL в Windows и вообще в других системах:

Основной синтаксис: Скрыть индикатор прогресса cURL: индикатор прогресса cURL позволяет нам детально видеть текущий прогресс загрузки, если мы хотим скрыть его, мы также можем использовать параметр -so –silent.

Таким образом, мы видели несколько альтернатив для установки и использования cURL в Windows PowerShell и в полной мере использовали эту команду.

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