Cara menggunakan curl windows server 2022

Curl (client URL) is a command-line tool powered by the libcurl library to transfer data to and from the server using various protocols, such as HTTP, HTTPS, FTP, FTPS, IMAP, IMAPS, POP3, POP3S, SMTP, and SMTPS. It is highly popular for automation and scripts due to its wide range of features and protocol support. In this article, you will learn how to use curl in Windows with various examples. Let’s get started.

Contents

Cara menggunakan curl windows server 2022

Surender Kumar

Surender Kumar has more than twelve years of experience in server and network administration. His fields of interest are Windows Servers, Active Directory, PowerShell, web servers, networking, Linux, virtualization, and penetration testing. He loves writing for his blog.

Cara menggunakan curl windows server 2022

Latest posts by Surender Kumar (see all)

  • Extending LVM space in Ubuntu - Thu, Feb 2 2023
  • Backup in Proxmox VE - Thu, Jan 26 2023
  • Snapshots in Proxmox VE - Wed, Jan 25 2023

Install curl on Windows

All the modern Windows versions, starting with Windows 10 (version 1803) and Server 2019, have the curl executable pre-installed, so there is no need for a manual installation. To determine the curl location and version in your system, you can use the following commands:

where curl
curl --version

Cara menggunakan curl windows server 2022

Determine the location and version of curl in Windows

The curl --version command also lists the protocols and features supported by the current curl version. If you see an output, as shown in the screenshot above, you’re all set to use the built-in curl utility. If you get an error message instead, curl might not be available, probably because you’re on an earlier version of Windows (e.g., Windows 8.1 or Server 2016). In that case, you might need to manually setup curl in Windows.

Curl syntax

The curl command uses the following syntax:

curl [options...] [url]

It supports various options, which we will discuss later in this post. As with any other command-line tool, you can use the curl --help command to get help.

Cara menggunakan curl windows server 2022

Getting help with the curl command

To get detailed help, you can use curl --help all. The help section is divided into categories, so the curl --help category gets you an overview of all the categories.

Now that you’ve become familiar with curl syntax, let’s discuss various use cases with the help of examples.

HTTP GET request

When you use curl against a URL without specifying any option, the request defaults to the GET method of the HTTP protocol. Try this:

curl https://4sysops.com

The above command is essentially equivalent to curl --request GET https://4sysops.com, which sends a GET request to 4sysops.com using the HTTPS protocol. To specify the HTTP protocol version (e.g., http/2), use the --http2 option, as shown below:

curl --http2 https://4sysops.com

For URLs starting with HTTPS, curl first tries to negotiate to establish a http/2 connection and automatically falls back to http/1.1 if the negotiation fails. It also supports other methods, such as HEAD, POST, PUT, and DELETE. To use these methods, along with the curl command, use the --request (or -X) option, followed by the method. Notice that the methods that are available depend on the protocol being used.

Get remote file information.

As an admin, you might want to be interested in HTTP headers only. This can be done using the --head (or -I) option. Sometimes, a URL might redirect you to another location. In that case, --location (or -L) allows the curl to follow the redirects. You can also use --insecure (or -k) to allow insecure connections to avoid any TLS certificate errors if the target URL is using a self-signed certificate. Use this only when absolutely necessary. All three of these options can be combined in short-notation, as shown in the following command:

curl -kIL 4sysops.com

Cara menggunakan curl windows server 2022

View request headers allow insecure connection and follow redirect options with curl

You can see that short-notation is particularly useful for combining multiple options. The above command is essentially equivalent to the curl --insecure --head --location 4sysops.com command.

The --head (or -I) option also gives you basic information about a remote file without actually downloading it. As shown in the screenshot below, when you use curl with a remote file URL, it displays various headers to give you information about the remote file.

curl -IL https://curl.se/windows/dl-7.85.0_5/curl-7.85.0_5-win64-mingw.zip

Cara menggunakan curl windows server 2022

Use curl to view the basic information about remote files

The Content-Length header indicates the size of the file (in bytes), Content-Type reveals the media type of the file (for instance image/png, text/htm), Server indicates the type of server application (Apache, Gunicron, etc.), Last-Modified shows the date when file was last changed on the server, and the Accept-Ranges header indicates the support of partial requests from the client for downloads, which essentially means you can resume an interrupted download.

Download a file

You can use curl with the --remote-name option (or -O, in short) to download a file and save it with the same name as on the server. The following command downloads the latest version of curl for Windows from the official website:

curl -OL https://curl.se/windows/latest.cgi?p=win64-mingw.zip

Cara menggunakan curl windows server 2022

Downloading a file with a default name and progress indicator using curl

The -L option is added to follow redirects, if needed, for locating the resource. If you want to save the file with a new name, use the --output (or -o) option instead. Furthermore, while using the curl command in a script, you might want to suppress the progress indicator using --silent (or -s). Both options can be combined, as shown in the following command:

curl -sLo curl.zip https://curl.se/windows/latest.cgi?p=win64-mingw.zip

Cara menggunakan curl windows server 2022

Silently download a file and save with a custom name using curl

Resume interrupted download

The presence of Accept-Ranges: bytes in the response header literally means that the server supports resumable downloads. To resume an interrupted download, you can use --continue-at (or -C), which accepts an offset (in bytes). Generally, specifying an offset is tricky, so curl offers an easy way of resuming an interrupted download:

curl -OLC - https://releases.ubuntu.com/22.04/ubuntu-22.04.1-desktop-amd64.iso 

Cara menggunakan curl windows server 2022

Resuming an interrupted download with curl

As you can see in the screenshot, I was downloading an Ubuntu iso file, which was interrupted. When I ran the curl command again with the -C option, the transfer was resumed from the byte range where it was interrupted. The minus sign (-) next to -C allows the curl to automatically figure out how and where to resume the interrupted download.

Authentication with Curl

Curl also supports authentication, allowing you to download a protected file by supplying credentials with the --user (or -u) option, which accepts a username and password in the username:password format. If you skip typing the password, curl will prompt you to type it in no-echo mode.

curl -u surender -OL https://techtutsonline.com/secretFiles/sample.zip

Cara menggunakan curl windows server 2022

Downloading a file using username and password authentication with curl

If you use a basic authentication method, you have to transfer a username and password, which means that you should use a secure protocol such as HTTPS (instead of HTTP) or FTPS (instead of FTP). If, for some reason, you have to use an unencrypted protocol, make sure you use an authentication method that doesn’t transmit credentials in clear text (for instance, Digest, NTLM, or Negotiate authentication).

Curl also supports the use of .curlrc, _curlrc, and .netrc config files, allowing you to define various curl options in a file and then to include the file in your command with curl --config (or curl -K), which is particularly useful for scripting.

Upload a file

The --upload-file (or -T) option allows you to upload a local file to a remote server. The following command shows how to upload a file from a local system to a remote web server using the FTPS protocol:

curl [options...] [url]
0

Cara menggunakan curl windows server 2022

Uploading a file to a remote server using curl

The -k option is included to avoid certificate errors if the web server uses a self-signed certificate. The trailing slash at the end of the URL tells curl that the destination is a directory. You could specify multiple file names, such as “{sample1.zip,sample2.zip}.” The following command shows how to upload multiple files with a single curl command:

curl [options...] [url]
1

Cara menggunakan curl windows server 2022

Upload multiple files to a remote server using curl

Quote a command

As already discussed, curl supports various methods based on the underlying protocol being used. You can send additional commands using --quote (or -Q) to perform a particular operation either before or after the regular curl operation; for instance, if you want to download a file from a remote server using the FTPS protocol and want the file to be removed from the server once it has been downloaded successfully. To do this, you can run the command shown below:

curl [options...] [url]
2

Cara menggunakan curl windows server 2022

Delete a file after successful download using curl command

Here, I downloaded the sample1.zip file from an FTPS server with the help of the -O option. After the -Q option, I added a minus sign (-) just before the DELE command, which tells the curl to send the DELE sample1.zip command immediately after the file is downloaded successfully. Likewise, if you want to send a command to the server before performing the actual curl operation, use a plus (+) sign instead of a minus sign.

Change the user-agent

The user-agent tells a server what type of client is sending the request. When you send a curl request to the server, the curl/<version> user-agent is used by default. If the server is configured to block the curl requests, you can specify a custom user-agent using --user-agent (or -A). The following command sends a common Google Chrome user-agent:

curl [options...] [url]
3

Cara menggunakan curl windows server 2022

Use a custom user agent with a curl command to avoid server blocks

The above screenshot shows that a normal curl request was forbidden by the web server (with a 403 Forbidden response), but when I passed a custom user-agent, the request was successful, returning a 200 OK response.

By default, the curl request does not send or store cookies. To write a cookie, use the --cookie-jar (or -c) option, and with --cookie (or -b), you can send a cookie:

curl [options...] [url]
4

The first command writes a cookie file, and the second command sends the cookie with a curl request. You can also send a cookie in 'name = value’' format, as shown below:

curl [options...] [url]
5

Cara menggunakan curl windows server 2022

Send multiple cookies using a curl command

I used the echo.hoppscotch.io website to view HTTP request headers that aren’t normally visible to clients sending a request. If you don’t want to use this website, you could use the –verbose (or -v) option to see your request in raw form (which will show request headers, too).

Use a proxy server

Do you use a proxy server to connect to the internet? No problem! Curl lets you specify a proxy server using the --proxy (or -x) option. If your proxy server requires authentication, add --proxy-user (or -U):

curl [options...] [url]
6

The proxy server is specified in the server:port format, and the proxy user is specified in the username:password format. Again, you could skip typing the password for the proxy user, and curl will prompt you to enter it in no-echo mode.

Cara menggunakan curl windows server 2022

Use a proxy server and authentication with a curl command

Additional request headers

Sometimes, you might want to send additional information along with your request to the server. With curl, you can do so easily by using --header (or -H), as shown in the following command:

curl [options...] [url]
7

Cara menggunakan curl windows server 2022

Specify additional headers with a curl request

You could send any information that isn’t available with standard HTTP request headers. In this example, I sent my operating system name. I also added the -v option this time to enable verbose output, which displayed the additional header being sent along with my curl request.

Send an email

Since curl supports the SMTP protocol, you could use it to send an email message. The following command shows how to send an email using curl:

curl [options...] [url]
8

Cara menggunakan curl windows server 2022

Send an email message using a curl command

Let’s quickly discuss the options used:

  • The --insecure (or -k) command is used to avoid an SSL certificate error. We have used this before.
  • The --ssl-reql option is used to upgrade a plain-text connection to encrypted connection if supported by the SMTP server. Alternatively, if you’re sure your SMTP server supports SSL, you could directly use the smtps server name (e.g., smtps://smtp.yourdomain.com), as you can see in the screenshot.
  • The --mail-from option is used to define the sender’s (from) email address.
  • The mail-rcpt option specifies the recipient's email address.
  • The --user (or -u) option sends the username for authentication, which should match the mail-from address, because otherwise your message might be rejected or flagged as spam.
  • The --upload-file (or -T) option is used to specify a file that contains the email message to send.

The following screenshot shows the email message I received in my inbox:

Cara menggunakan curl windows server 2022

Viewing the email message sent with curl

Viewing the email message sent with curl

Subscribe to 4sysops newsletter!

These are just a few examples, but there is a lot more you can do with curl. I highly recommend checking out curl help and experimenting with it. You'll notice what a powerful command curl is.

cURL untuk apa?

Secara umum cURL berfungsi untuk melakukan konektivitas atau alat (tools) transfer data, pada artikel kali ini kita akan membahas tentang, apa itu cURL, fungsi dan bagaimana cara kira melakukan pengecekan apakah cURL pada hosting atau server sudah aktif.

Langkah instalasi cURL?

Cara Install dan Menggunakan cURL pada Windows.
Download cURL di curl.se/windows/. ... .
Ekstrak cURL yang tadi sudah Anda download. ... .
Agar bisa digunakan di Command Prompt (cmd) kita perlu memasukkan cURL ke dalam path terlebih dahulu. ... .
Setelah itu pilih Environment Variables..

Apa itu cURL api?

Curl adalah “alat baris perintah untuk mentransfer data yang ditentukan dengan sintaks URL”, yang membuatnya berguna untuk berinteraksi dengan REST APIs dan sumber situs lainnya.