There are two ways in which you can perform basic auth using cURL,
- Using the -u or --user option
- Using the Authorization header in the request
Using the -u or --user option
In other to perform Basic Authentication using cURL command make use of -u or the --user option followed by user:password along with the cURL command request.
Syntax:curl -u <user-name:password> the-url
Example:% curl -u newuser:mypassword https://example.org
Note: If your username or password contains special characters, make sure the wrap the 'user:password' within single quotes.
Example: Basic Auth cURL with special character is user or password% curl -u 'new_user:my$password' https://example.org
Note: As the colon is used as a separator to distinguish the username and password, you cannot have a colon as a character in the username, but you can have it in the password string.
If you do not provide the password with the -u option and just provide the user, cURL will prompt you to enter the password.
Example: cURL Basic Auth with password prompt% curl -u newuser https://example.org
Enter host password for user 'newuser':
Perform Basic Auth by passing Authorization Header in cURL Request
The username:password string needs to be Base64-Encoded and passed in as a Header to your cURL request as below,
Plan text user:passwordmyuser:my$password
Base64 Encoded user:password
bXl1c2VyOm15JHBhc3N3b3Jk
cURL Example using Authorization Header
curl 'https://example.org' \
--header 'Authorization: Basic bXl1c2VyOm15JHBhc3N3b3Jk
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- cURL Option to show HTTP Response Status Code
- URL Encode cURL HTTP GET\POST Data with Examples
- Capture cURL Request Output to a File
- Fix: Curl No Match Found Error
- trurl: A new command-line tool for URL parsing and manipulation by cURL Developer
- Send JSON Data as Payload using cURL HTTP POST Request
- cURL: Show Request and Response Headers
- Get cURL command from Chrome Inspect Network HTTP URL
- Save cURL Command Output to a external file
- cURL DELETE Request with Examples
- How to URL Encode Data for a cURL Request Parameters
- cURL Example using IP (IPv4 and IPv6) address and Port
- How to convert a Postman request to cURL Command
- [fix] command not found curl
- How to Pretty Print cURL JSON Output in Terminal
- How to follow redirects in cURL Command Request
- How to pass authorization header using cURL
- cURL -x Use of HTTP Proxy Server Examples
- Downloading Google Chrome using cURL Command
- How to get cURL Command to run in verbose mode?
- List of 28 Protocols supported by cURL with Examples
- Hide cURL Outputs and Errors on Terminal
- cURL -d Option with Examples
- How to show SSL Certificate details using cURL Command
- Perform Basic Authentication using cURL with Examples
- Command to know the installed Debian version? - Linux
- Make Android View Scrollable both Horizontally and Vertically - Android
- How to Know Version of Windows Terminal - Windows
- Installing vue.js in Laravel 8 - PHP
- Python: Determine Variable Type Example - Python
- How to format or prettify XML in Notepad++ - NotepadPlusPlus
- How to Subscribe to Google Nest Aware on iPhone - Google
- Get count of SharePoint List or Document Library Items using PowerShell - SharePoint