Setting Content-Type for Curl Request [PHP Code]

To send the Content-Type header using Curl, you need to use the -H command-line option. For example, you can use the -H "Content-Type: application/json" command-line parameter for JSON data. Data is passed to Curl using the -d command-line option. It must match the provided content type. In this Curl Content-Type example, we are sending JSON to the ReqBin echo URL. Click Run to execute the Curl Content-Type example online and see the results. The PHP code was automatically generated for the Curl Content Type example.
Setting Content-Type for Curl Request [PHP Code] Run
curl -X POST https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -H 'Accept: application/json'
   -d '{"Id": 78912, "Quantity": 1, "Price": 19.00}'
Updated: Viewed: 57511 times
PHP code for Curl Content Type example

PHP code for Curl Content Type Example

This PHP code snippet was generated automatically for the Curl Content Type example.
<< Back to the Curl Content Type example

What is Curl?

Curl is an open-source command-line tool and a cross-platform library (libcurl) that allows users to make requests from clients to servers. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and SFTP. Curl has built-in support for SSL, certificate validation, HTTP Cookies support, and user authentication. Curl works on Linux, Windows, and macOS.

What is Content Type?

The Content-Type header is used to specify the media type of the resource in the HTTP entity. The content type is specified according to MIME (Multipurpose Email Extensions), which are standardized and published by the IANA. The Content-Type header defines the nature of the data in the body of the HTTP message by specifying the type and subtype identifiers and optionally additional parameters for specific data types. A set of additional parameters is passed in key/value format. For example, when sending a PNG image to the server, the browser specifies the content type of the request as Content-Type: image/png. If the server returns an HTML document, the response Content-Type will be text/html.

Content-Type Example
Content-Type: image/png
Content-Type: text/html; charset=UTF-8
Content-Type: multipart/form-data;
boundary=---Q3d4fD"

How can I pass the Content-Type header using Curl?

When you send data to the server using Curl by making a POST, PUT, or PATCH request, you must also specify the data type in the body of the message using the Content-Type header. This is important and allows the server to receive, interpret and process the received data correctly. To pass a Content-Type header to Curl, use the -H command-line option.

Curl Content-Type Example
curl https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -d '{"Id": 78912, "Quantity": 1,
"Price": 19.00}'

Curl Content-Type Syntax

The syntax for specifying the data type for a Curl request is:

Curl Content-Type Example
curl -H "Content-Type: mime type"
   -d "[request data]"
   [options]

Where:
  • -H, —header: the HTTP header with which contains the data type for the data in the request body
  • d, —data: data to send to server using POST, PUT, or PATCH request.

Why do I need to explicitly specify the Content-Type when posting data using Curl?

If you are submitting data using Curl and you do not explicitly specify the content type, Curl uses the application/x-www-form-urlencoded content type for your data. This is what your browser typically uses when submitting an HTML form. If this title doesn't suit you, you need to replace it with the correct one. For example, if you send JSON to the server, you need to specify the data type using the -H "Content-Type: application/json" command line parameter.

Why is it important to specify the correct Content-Type when submitting JSON?

If you don't set the correct Content-Type, your app may not work. For example, the target server can accept XML and JSON data on the same API endpoint. Setting Content-Type to application/json will let the server know that the client is sending JSON data, and application/xml will tell the server that the client is sending XML.

Curl Content-Type Example for JSON Data

An example of sending the Content-Type header for JSON data:

Content-Type JSON Example
curl -X POST https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -d '{"id": 1}'

Curl Content-Type Example for XML Data

An example of sending the Content-Type header for XML data:

Content-Type JSON Example
curl -X POST https://reqbin.com/echo/post/xml
   -H 'Content-Type: application/xml'
   -d "1"

See also

Generate code snippets for PHP and other programming languages

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