How to set the authorization header using cURL
CurlHttp AuthenticationCurl Problem Overview
How do I pass authorization header using cURL? ( executable in /usr/bin/curl
).
Curl Solutions
Solution 1 - Curl
http://curl.se/docs/httpscripting.html
See part 6. HTTP Authentication
> HTTP Authentication
> HTTP Authentication is the ability to tell the server your username and password so that it can verify that you're allowed to do the request you're doing. The Basic authentication used in HTTP (which is the type curl uses by default) is plain text based, which means it sends username and password only slightly obfuscated, but still fully readable by anyone that sniffs on the network between you and the remote server.
> To tell curl to use a user and password for authentication:
> curl --user name:password http://www.example.com
> The site might require a different authentication method (check the headers returned by the server), and then --ntlm, --digest, --negotiate or even --anyauth might be options that suit you.
> Sometimes your HTTP access is only available through the use of a HTTP proxy. This seems to be especially common at various companies. A HTTP proxy may require its own user and password to allow the client to get through to the Internet. To specify those with curl, run something like:
> curl --proxy-user proxyuser:proxypassword curl.haxx.se
> If your proxy requires the authentication to be done using the NTLM method, use --proxy-ntlm, if it requires Digest use --proxy-digest.
> If you use any one these user+password options but leave out the password part, curl will prompt for the password interactively.
> Do note that when a program is run, its parameters might be possible to see when listing the running processes of the system. Thus, other users may be able to watch your passwords if you pass them as plain command line options. There are ways to circumvent this.
> It is worth noting that while this is how HTTP Authentication works, very many web sites will not use this concept when they provide logins etc. See the Web Login chapter further below for more details on that.
Solution 2 - Curl
Just adding so you don't have to click-through:
curl --user name:password http://www.example.com
or if you're trying to do send authentication for OAuth 2:
curl -H "Authorization: OAuth <ACCESS_TOKEN>" http://www.example.com
Solution 3 - Curl
Bearer tokens look like this:
curl -H "Authorization: Bearer <ACCESS_TOKEN>" http://www.example.com
Solution 4 - Curl
This worked for me:
curl -H "Authorization: Bearer xxxxxxxxxxxxxx" https://www.example.com/
Solution 5 - Curl
(for those who are looking for php-curl answer)
$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
Solution 6 - Curl
For HTTP Basic Auth:
curl -H "Authorization: Basic <_your_token_>" http://www.example.com
replace _your_token_
and the URL.
Solution 7 - Curl
Be careful that when you using:
curl -H "Authorization: token_str" http://www.example.com
token_str
and Authorization
must be separated by white space, otherwise server-side will not get the HTTP_AUTHORIZATION
environment.
Solution 8 - Curl
If you don't have the token at the time of the call is made, You will have to make two calls, one to get the token and the other to extract the token form the response, pay attention to
> grep token | cut -d, -f1 | cut -d\" -f4
as it is the part which is dealing with extracting the token from the response.
echo "Getting token response and extracting token"
def token = sh (returnStdout: true, script: """
curl -S -i -k -X POST https://www.example.com/getToken -H \"Content-Type: application/json\" -H \"Accept: application/json\" -d @requestFile.json | grep token | cut -d, -f1 | cut -d\\" -f4
""").split()
After extracting the token you can use the token to make subsequent calls as follows.
echo "Token : ${token[-1]}"
echo "Making calls using token..."
curl -S -i -k -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${token[-1]}" https://www.example.com/api/resources
Solution 9 - Curl
This example includes the following:
- POST request
- Header Content-Type
- Header Authorization
- Data flag with JSON data
- Base64 encoded token
- Ref-1: curl authorization header
- Ref-2: curl POST request
curl -X POST -H "Content-Type: application/json" -d '{"name”:”Johnny B. Goode”, "email”:”[email protected]"}' -H "Authorization: Bearer $(echo -n Guitar Maestro | base64)" https://url-address.com
Solution 10 - Curl
As of curl
7.61.0 you can use the --oauth2-bearer <token>
option to set the correct Bearer authorization headers.
Solution 11 - Curl
For those doing Token-Based authentication ... make sure you do :
curl -H "AuthToken:
instead !!
Solution 12 - Curl
A simple example is using parameters with authorization converted to base64
curl -XPOST 'http://exemplo.com/webhooks?Authorization=Basic%20dGVzdDoxMjM0NTYK'