Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH

Bash curl - Transfer a URL

Using the curl Command

The curl command is used to transfer data from or to a server using various protocols like HTTP, HTTPS, FTP, and more.

It's a versatile tool for downloading files, testing APIs, and more.


Basic Usage

To retrieve a web page, use curl url:

Example

curl http://example.com/file.txt
Hello, this is a test file.
There are three lines here.
This is the last line.

Options

The curl command has options to change how it works:

  • -O - Save the file with the same name as the remote file
  • -L - Follow redirects
  • -I - Fetch the HTTP headers only
  • -d - Send data with POST request
  • -u - Specify user and password for server authentication

Save the File with the Same Name as the Remote File

The -O option allows you to save the file with the same name as the remote file.

This is useful for downloading files directly to your local system with their original names.

Example: Save the File with the Same Name as the Remote File

curl -O http://example.com/file.txt
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   134  100   134    0     0    216      0 --:--:-- --:--:-- --:--:--   218

Follow Redirects

The -L option allows you to follow redirects.

This is useful when accessing URLs that may redirect to another location.

Example: Follow Redirects

curl -L http://example.com/redirect
Hello, this is a test file.
There are three lines here.
This is the last line.

Fetch the HTTP Headers Only

The -I option allows you to fetch the HTTP headers only.

This is useful for checking server response headers without downloading the entire content.

Example: Fetch the HTTP Headers Only

curl -I http://example.com
# Output:
# HTTP/1.1 200 OK
# Date: Wed, 10 Apr 2025 10:00:00 GMT
# Content-Type: text/html; charset=UTF-8
# Connection: keep-alive

Send Data with POST Request

The -d option allows you to send data with a POST request. This is useful for submitting form data or interacting with APIs.

Example: Send Data with POST Request

curl -d "fname=John" https://www.example.com/action_page.php

Submitted Form Data

Your input was received as:

fname=John 

Specify User and Password for Server Authentication

The -u option allows you to specify a user and password for server authentication. This is useful for accessing protected resources.

Example: Specify User and Password for Server Authentication

curl -u user:password http://example.com/protected
Hello, this is a test file.
There are three lines here.
This is the last line.

Understanding curl Output

The curl command output will vary depending on which options are used:

  • HTTP Status Code: Indicates the success or failure of the request.
  • Response Headers: Provide metadata about the server response.
  • Response Body: The actual content retrieved from the server.
  • Progress Meter: Shows download progress and speed.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.