Sending Curl POST Request with Basic Authentication [PHP Code]

To send a POST request with basic authentication credentials with Curl, you need to use the --user "login: password" command-line option. The user's credentials are automatically converted by Curl to a Base64 encoded string and passed to the server with an Authorization: Basic [token] header. POST data is passed to Curl with the -d option. In this Curl POST with Basic Authentication header example, we sent a request to the ReqBin echo URL with sample POST data. Click Run to execute the Curl POST Basic Auth example online and see the result. The PHP code was automatically generated for the Curl POST Basic Authentication example.
Sending Curl POST Request with Basic Authentication [PHP Code] Run
curl -X POST https://reqbin.com/echo/post/json
   -H "Content-Type: application/json" 
   -d '{"login":"my_login","password":"my_password"}'
   --user "login:password"     
Updated: Viewed: 57602 times
PHP code for Curl POST Basic Authentication example

PHP code for Curl POST Basic Authentication Example

This PHP code snippet was generated automatically for the Curl POST Basic Authentication example.
<< Back to the Curl POST Basic Authentication example

What is Curl?

Curl (stands for Client URL) is popular command-line tool developers use to send requests to the server, upload files, and submit web forms. Curl supports all modern protocols, including HTTP, HTTPS, SFTP, FTP, and has built-in support for SSL, user authentication, and HTTP Cookies. Curl works on Linux, Mac, Windows.

What is HTTP POST?

POST is one of the nine standard methods of the HTTP protocol. The HTTP POST method requests the webserver to receive and process the data contained in the body of the POST message. POST method is often used to submit login or contact forms and upload files and images to the server.

Curl POST Request Syntax

The general form of a Curl command for making a POST request is as follows:

Curl POST Request Syntax
curl -X POST [URL]
     -H [header] 
     -d [post_data]

Where:
  • -X, --request: HTTP method to use when communicating with the server
  • -H, --header: HTTP headers to send to the server with a POST request
  • -d, --data: Data to be sent to the server using a POST request

What is Basic Authentication?

Basic Authentication is a client authentication method built into the HTTP protocol that allows a client to provide a username and password to the server when accessing secure resources over HTTP. When requesting a protected resource, the client sends HTTP requests with an Authorization header that contains the word Basic followed by a space and a base64 encoded username: password string. Basic Authentication is not the most secure method because other protocol sniffers can easily decrypt base64 encoded user credentials. For security reasons, the Basic Authentication method should only be used over secure HTTPS/SSL connections.

How do I post Basic Authentication data using Curl?

To post a Curl request with Basic Authorization credentials, you can use the -u (or --user) command line parameter: --user username: password.

Curl Basic Authentication Request Example
curl --user "login:password" [URL]

Curl automatically converts the provided login: password pair into a Base64-encoded string and adds an appropriate HTTP header to the request:

"Authorization: Basic bG9naW46cGFzc3dvcmQ="

Example of Basic user Authentication using Curl POST request

The general form of a Curl command for making a POST request with Basic Authentication is as follows:

Curl POST Request with Basic Authentication Example
curl -X POST [URL]
   -H "Content-Type: application/json" 
   -d "{post_data}"
   --user "login:password"

Where:
  • -X: HTTP method to use when communicating with the server.
  • -H: HTTP header to send to the server with a POST request.
  • -d: Data to be sent to the server using a POST request.
  • --user: Provide the username and password that will be used to authenticate the server.

See also

Generate code snippets for PHP and other programming languages

Convert your Curl POST Basic Authentication request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.