Posting JSON with Bearer Token Authorization Header [PHP Code]

To post JSON with a Bearer Token Authorization header using PHP, you need to make an HTTP POST request, provide your Bearer Token with an "Authorization: Bearer {token}" HTTP header, and give the JSON data in the body of the POST message. The "Accept: application/json" header tells the server that the client expects JSON from the server. For security reasons, bearer tokens are only sent over HTTPS (SSL). In this PHP POST JSON with Bearer Token Authorization Header example, we send a request to the ReqBin echo URL with Authorization: Bearer {token} HTTP header. Click Send to execute the PHP POST JSON request with a Bearer Token Authorization Header example online and see results. The PHP code was automatically generated for the POST JSON Bearer Token Authorization Header example.
Posting JSON with Bearer Token Authorization Header [PHP Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Authorization: Bearer {token}
Content-Type: application/json
Content-Length: 61

{
	"employee":{ "name":"Emma", "age":28, "city":"Boston" }
} 
Updated: Viewed: 78969 times
PHP code for POST JSON Bearer Token Authorization Header example

PHP code for POST JSON Bearer Token Authorization Header Example

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

What it JSON?

JSON (JavaScript Object Notation) is a lightweight text-based, language-independent data exchange format that specifies formatting rules for the portable representation of structured data. JSON is self-describing, easy to understand, and language independent. JSON is used for client/server communication in web and mobile applications.

What is HTTP POST?

The HTTP POST method is the most commonly used of the nine standard Hypertext Transfer Protocol (HTTP) methods. The POST method requests the webserver to receive and process the data contained in the body of the POST message. The POST request is usually used when submitting an HTML form or when uploading data to a server. Unlike GET and HEAD requests, the HTTP POST requests may change the server state.

What is Bearer Authentication?

HTTP provides a framework for user authentication to control access to protected resources. Bearer authentication (token authentication) is done by sending security tokens in the authorization header. You can do bearer authentication with any programming language. The Bearer Token is a string with no meaning or uses but becomes important within a proper tokenization system. The server usually generates the bearer token in response to a login request and saves it in the browser or local storage. For security reasons, the bearer token should only be sent over HTTPS (SSL) connections.

Bearer Token Authentication Syntax

Below is the syntax of the Bearer Token Authentication header:

Bearer Token Authentication Header Syntax
Authorization: Bearer {token}

How to send JSON request with Bearer Token Authentication?

The following is an example of making a POST JSON request with a Bearer Token authentication header:

POST JSON With Bearer Token Authentication Example
POST /echo/post/json HTTP/1.1
Authorization: Bearer {token}
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 61
      
{
  "employee":{ "name":"Emma", "age":28, "city":"Boston" }
}

See also

Generate code snippets for PHP and other programming languages

Convert your POST JSON 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.