Sending Curl Request with Bearer Token [PHP Code]

To send a Bearer Token to the server using Curl, you can use the -H "Authorization: Bearer {token}" authorization header. The Bearer Token is an encrypted string that provides a user authentication framework to control access to protected resources. To send a Curl POST request, you need to pass the POST data with the -d command line option, and the authorization header and bearer token are passed with the -H command line option. In this Curl Request With Bearer Token Authorization Header example, we send a GET request to the ReqBin echo URL. Click Run to execute the Curl Bearer Token Authorization Header request online and see the results. The PHP code was automatically generated for the Curl Bearer Token Authorization Header example.
Sending Curl Request with Bearer Token [PHP Code] Run
curl https://reqbin.com/echo/get/json
   -H "Accept: application/json"
   -H "Authorization: Bearer {token}"
Updated: Viewed: 96364 times
PHP code for Curl Bearer Token Authorization Header example

PHP code for Curl Bearer Token Authorization Header Example

This PHP code snippet was generated automatically for the Curl Bearer Token Authorization Header example.
<< Back to the Curl Bearer Token Authorization Header example

What is Curl?

Curl is a well-known command-line tool for transferring data between servers, designed to work without user intervention. Curl can upload or download data using popular protocols including HTTP, HTTPS, SCP, SFTP, and FTP with Curl. Curl is used for API testing, has built-in support for proxies, SSL, HTTP cookies. Curl runs on Linux, Windows, and macOS platforms.

What is the Authorization Header?

HTTP provides a built-in framework for controlling access and authentication to protected resources. The authorization request header contains the credentials for authenticating the HTTP client to the server. The most commonly used authorization headers are Basic Auth and Bearer Token headers.

Authorization Header Syntax
Authorization: Basic {base64string}
Authorization: Bearer {token}

What is the Bearer Authorization Token?

HTTP provides a user authentication framework to control access to protected resources. Bearer authentication (also called token authentication) is done by sending security tokens in the authorization header. The Bearer Token is a string that is not intended to be used by clients. Some servers will issue bearer tokens, short lines of hexadecimal characters, while others may use structured tokens like JWTs. The client must send this token back to the server in every authorization header when requesting protected resources.

How to send Curl request with Bearer Authorization Header?

The Bearer Token can be sent in the Curl request header. To pass the bearer token in the Curl authorization header, add the following command line parameter when executing the Curl request:

Curl Authorization Header Example
-H "Authorization: Bearer {token}"

Curl Request with Bearer Token Examples

The following are examples of sending a request with a Bearer Authorization Header in Curl:

Sending Curl request with Bearer Authorization Header

The following is an example of sending a Curl request with Bearer Authorization Header in Curl:

Curl Request with Bearer Token Example
curl https://reqbin.com/echo/get/json
     -H "Authorization: Bearer {token}"

Sending Curl POST request with Bearer Token Authorization Header

Sending the Bearer Token with a Curl POST request is similar to sending the Bearer Token with a Curl GET request. POST data is passed with the -d command-line option, and the authorization header and the bearer token are passed with the -H command-line option.

Curl POST request with a Bearer Token Example
curl -X POST https://reqbin.com/echo/get/json
     -H "Authorization: Bearer {token}"
     -d "[post data]"

See also

Generate code snippets for PHP and other programming languages

Convert your Curl Bearer Token Authorization Header request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.