mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Authorize A cURL Request as a Unix TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1651 TILs and counting..._
|
||||
_1652 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
- [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7)
|
||||
@@ -1534,6 +1534,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
### Unix
|
||||
|
||||
- [All The Environment Variables](unix/all-the-environment-variables.md)
|
||||
- [Authorize A cURL Request](unix/authorize-a-curl-request.md)
|
||||
- [Cat A File With Line Numbers](unix/cat-a-file-with-line-numbers.md)
|
||||
- [Cat Files With Color Using Bat](unix/cat-files-with-color-using-bat.md)
|
||||
- [Change Default Shell For A User](unix/change-default-shell-for-a-user.md)
|
||||
|
||||
32
unix/authorize-a-curl-request.md
Normal file
32
unix/authorize-a-curl-request.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Authorize A cURL Request
|
||||
|
||||
When making a cURL request to an endpoint that requires authentication,
|
||||
sometimes you already have a bearer token and other times you have a username
|
||||
and password pair. If you have a bearer token, you can format a `Authorization`
|
||||
header with the `-H` flag that includes that value.
|
||||
|
||||
If you have a username and password for the API, you can instead use the `-u`
|
||||
flag. The `-u` flag will format the username and password, base64 encode it,
|
||||
and then add it as an `Authorization` header.
|
||||
|
||||
```bash
|
||||
$ curl -v -u "username:password" https://some-endpoint.com/api/v1/status
|
||||
|
||||
...
|
||||
> GET /api/v1/status HTTP/2
|
||||
> Host: some-endpoint.com
|
||||
> Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQK
|
||||
> User-Agent: curl/8.1.2
|
||||
...
|
||||
```
|
||||
|
||||
You can even pass in only the username to the `-u` flag and cURL will know to
|
||||
prompt you for the password. This is a nice way to avoid putting plain text
|
||||
passwords in your shell history.
|
||||
|
||||
```bash
|
||||
$ curl -v -u "username" https://some-endpoint.com/api/v1/status
|
||||
Enter host password for user 'username':
|
||||
```
|
||||
|
||||
See `man curl` for more details.
|
||||
Reference in New Issue
Block a user