GICT knowledge base
How to send Authenticated Requests to API using cURL
Published
cURL is a tool you can use on your computer. It helps you get data from websites. You type commands and it talks to websites for you.
Basic Auth means you show your username and password. The website checks if they are right. If yes, it gives you the data you want.
Think of it like going to a bank. You show your ID card. The bank person checks it. Then they help you with your money.
Why People Use It
Many websites need you to login first. They want to know who you are before giving data. This keeps the data safe from bad people.
You can use cURL Basic Auth to test websites. You can check if your login works. You can see what data comes back.
It works well for simple jobs. You can write small programs that get data by themselves.
How to Set Up cURL request with Basic Auth Login
The easy way looks like this:
curl -u john:pass123 https://website.com/data
You change “john” to your username. You change “pass123” to your password. You change the website part too.
Here is a real example:
curl -u mary:secret456 https://api.store.com/products
This logs in as mary. It uses password secret456. Then it gets product data.
Sometimes you do not want others to see your password. You can do this:
curl -u mary https://api.store.com/products
The computer will ask you to type your password. Nobody else can see it.
You can also save your login info first:
export USER="mary"
export PASS="secret456"
curl -u $USER:$PASS https://api.store.com/products
This way your password stays hidden.
Basic Commands
Getting data from websites:
Get all products:
curl -u mary:secret456 https://api.store.com/products
Get one specific product:
curl -u mary:secret456 https://api.store.com/products/10
Sending new data:
Add a new product:
curl -u mary:secret456 -d "name=Apple&price=2" https://api.store.com/products
The -d part sends your data to the website.
Changing existing data:
Update product number 10:
curl -u mary:secret456 -X PUT -d "name=Red Apple&price=3" https://api.store.com/products/10
Removing data:
Delete product number 10:
curl -u mary:secret456 -X DELETE https://api.store.com/products/10
Keeping Things Safe
Always use websites that start with “https” not “http”:
curl -u mary:secret456 https://safe-website.com/data
The “s” means secure. Your password gets locked so hackers cannot steal it.
Never write your real password where others can see it. Use fake names or hide it in safe places.
Some websites give you special codes instead of passwords:
curl -u "xyz789code:" https://website.com/data
See the colon after the code? You do not need a password with these codes.
Adding Extra Info
Sometimes websites want more details. You can add them:
curl -u mary:secret456 -H "Content-Type: application/json" https://website.com/data
This tells the website what kind of data you are sending.
You can add many details:
curl -u mary:secret456 -H "Content-Type: application/json" -H "Language: English" https://website.com/data
Set a time limit so you do not wait forever:
curl -u mary:secret456 --timeout 30 https://website.com/data
This stops trying after 30 seconds.
Save the answer to a file:
curl -u mary:secret456 https://website.com/data -o myfile.txt
Finding Problems
When things go wrong, use -v to see more:
curl -v -u mary:secret456 https://website.com/data
This shows you everything that happens.
Error numbers tell you what went wrong:
- 401 means wrong username or password
- 403 means you cannot access this data
- 404 means the page does not exist
- 500 means the website is broken
Check just the error number:
curl -u mary:secret456 -w "%{http_code}" https://website.com/data
Real Examples
Get your GitHub projects:
curl -u yourname:yourtoken https://api.github.com/user/repos
GitHub gives you a token instead of using your password.
Download a file:
curl -u mary:secret456 https://website.com/report.pdf -o myreport.pdf
This saves a PDF file to your computer.
Check if website works:
curl -u mary:secret456 https://website.com/status
Send data in JSON format:
curl -u mary:secret456 -H "Content-Type: application/json" -d '{"name":"Bob","age":30}' https://api.website.com/users
Safety Rules
- Never share your passwords with other people.
- Change your passwords often. Do this more if you think someone knows them.
- Use different passwords for different websites.
- Watch your accounts for strange things happening.
- Do not write passwords in files that others can read.
- Keep your computer safe with good security software.
Other Ways to Login
Basic Auth is simple but not always the safest. Other ways exist:
Token method:
curl -H "Authorization: Bearer abc123token" https://website.com/data
API key method:
curl -H "X-API-Key: key456def" https://website.com/data
These ways are safer because you can change or stop them easily.
When to Use Basic Auth
Good times to use it:
- Testing new websites
- Learning how things work
- Simple one-time jobs
- When websites ask for it
Bad times to use it:
- Important business apps
- Money-related websites
- Personal info of other people
- Public programs many people use
Getting Started
Start with practice websites that use fake data. You cannot break anything this way.
Try a simple command first:
curl -u test:test https://practice-api.com/data
When that works, try sending data:
curl -u test:test -d "hello=world" https://practice-api.com/data
Practice different commands until you feel good about them.
Then try with real websites using your real login.
Save commands that work in a text file. You can use them again later.
Common Problems and Fixes
Problem: Computer says “command not found”
Fix: Install cURL on your computer first. If you are using Ubuntu, Open terminal and execute sudo apt-get install curl -y to quickly install cURL on your system.
Problem: Always get 401 errors
Fix: Check your username and password are correct.
Problem: Takes too long
Fix: Add –timeout 30 to your command.
Problem: Cannot see what happened
Fix: Add -v to see more details.
Problem: Password has weird characters
Fix: Put your password in quotes like “my@pass#word”.
Problem: Website says method not allowed
Fix: Add -X POST or -X PUT to your command.
Tips for Success
- Start small and simple. Do not try complex things right away.
- Test everything with fake data first.
- Keep your passwords secret and safe.
- Use https websites when possible.
- Learn one command at a time.
- Ask for help when you get stuck.
- Practice makes you better.
Summary
cURL Basic Auth helps you talk to websites from your computer. You give your username and password. The website checks them. Then it gives you data.
It works well for testing and simple jobs. But use safer methods for important work.
Remember these key points:
- Use https to stay safe
- Keep passwords secret
- Test with fake data first
- Start simple and learn slowly
- Use better methods for important stuff
With practice, you can get data from any website that uses Basic Auth. Take your time and be careful with your passwords.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
