Lesson 27 of 158 – Content-Type
27%

Content-Type

The Content-Type HTTP header tells the server what type of data is being sent in the request body. It is especially important when a React Native application sends JSON data to a REST API.

For example, when a mobile application sends a JSON request body, it can use Content-Type: application/json. This tells the server that the body contains JSON data.

Note: Content-Type describes the media type of the message body. It is different from the Accept header, which tells the server what response format the client wants.

1. What is Content-Type?

Content-Type is an HTTP header that identifies the media type of the data contained in the request or response body.


Content-Type: application/json

This tells the receiving application that the body contains JSON.

2. Why is Content-Type Important?

The server needs to know how to interpret the data it receives. Content-Type provides this information.

For example:


Content-Type: application/json

The server knows that the request body should be interpreted as JSON data.

3. Content-Type and Request Body

Content-Type is commonly used together with a request body.


POST /api/students

Content-Type: application/json

{
    "name": "Rahul",
    "course": "React Native"
}

The header describes the format of the body below it.

4. application/json

application/json is the media type commonly used when sending JSON data.


Content-Type: application/json

This is one of the most important Content-Type values when building REST APIs for React Native applications.

5. JSON Request Example


POST /api/students
Content-Type: application/json

{
    "name": "Rahul",
    "email": "rahul@example.com",
    "course": "React Native"
}

The server can identify the request body as JSON because of the Content-Type header.

6. Content-Type in Fetch

When using Fetch to send JSON, the Content-Type header can be specified inside the headers object.


const response = await fetch(
    "https://example.com/api/students",
    {
        method: "POST",

        headers: {
            "Content-Type": "application/json"
        },

        body: JSON.stringify({
            name: "Rahul",
            course: "React Native"
        })
    }
);

7. Content-Type with Axios

Axios can send JSON data in a POST request.


const response = await axios.post(
    "https://example.com/api/students",
    {
        name: "Rahul",
        course: "React Native"
    }
);

Axios commonly handles the appropriate JSON content type when sending a JavaScript object, although headers can also be configured explicitly.

8. Setting Content-Type Explicitly in Axios

The Content-Type header can also be explicitly specified in Axios.


const response = await axios.post(
    "https://example.com/api/students",

    {
        name: "Rahul",
        course: "React Native"
    },

    {
        headers: {
            "Content-Type": "application/json"
        }
    }
);

9. Content-Type in Postman

When using Postman to send raw JSON, the request can be configured as JSON.

  1. Select POST.
  2. Enter the API URL.
  3. Open the Body tab.
  4. Select raw.
  5. Select JSON.
  6. Enter the JSON body.
  7. Send the request.

Postman can then send the request with the appropriate JSON media type.

10. Content-Type and PHP

A PHP REST API receives the HTTP request and can inspect its headers when processing incoming data.

For JSON requests, the body can be read using:


$input = file_get_contents(
    "php://input"
);

$data = json_decode(
    $input,
    true
);

The JSON body can then be validated and processed.

11. Content-Type: text/plain

The text/plain media type indicates plain text data.


Content-Type: text/plain

For example, the body might contain:


Hello from the API

12. Content-Type: text/html

The text/html media type indicates HTML content.


Content-Type: text/html

This tells the receiving application that the body contains HTML markup.

13. Content-Type: application/xml

The application/xml media type can be used when the body contains XML data.


Content-Type: application/xml

REST APIs can support different data formats, although JSON is very common in modern mobile applications.

14. Content-Type: application/x-www-form-urlencoded

The application/x-www-form-urlencoded media type represents form-style key-value data encoded in the request body.


Content-Type:
application/x-www-form-urlencoded

The body can look similar to:


name=Rahul&course=React%20Native

15. Content-Type: multipart/form-data

The multipart/form-data media type is commonly used for form submissions that may include files.


Content-Type: multipart/form-data

It is particularly useful for file uploads such as images or documents.

16. JSON vs Form Data

JSON Form Data
application/json multipart/form-data
Structured JSON objects Form fields and files
Common REST API format Common for file uploads

17. Content-Type vs Accept

Content-Type and Accept have different purposes.

Header Purpose
Content-Type Describes the format of the body being sent.
Accept Indicates the response formats the client can accept.

For example:


Content-Type: application/json
Accept: application/json

18. Content-Type in API Response

Content-Type can also describe the body of an HTTP response.


HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "message": "Data found"
}

Here, the response body is JSON.

19. Setting JSON Content-Type in PHP

A PHP REST API can explicitly set the Content-Type of a JSON response.


header(
    "Content-Type: application/json"
);

echo json_encode([
    "success" => true,
    "message" => "Success"
]);

This tells the client that the response body contains JSON.

20. Content-Type and JSON Response

A well-formed JSON API response commonly includes the JSON media type.


Content-Type: application/json

The response body may contain:


{
    "success": true,
    "data": []
}

21. Content-Type with Character Encoding

A media type can include additional parameters when appropriate. For example, a text media type can specify a character encoding.


Content-Type: text/plain; charset=UTF-8

For JSON, modern JSON handling uses UTF-8 as the standard encoding for interoperable JSON data.

22. Checking Content-Type in PHP

PHP applications can inspect incoming HTTP headers when they need to check how the request body is formatted.


$contentType =
    $_SERVER['CONTENT_TYPE'] ?? '';

echo $contentType;

The exact server environment can affect how request headers are exposed to PHP.

23. Validating JSON Content-Type

An API can check whether a request is intended to contain JSON before processing it.


$contentType =
    $_SERVER['CONTENT_TYPE'] ?? '';

if (
    stripos(
        $contentType,
        'application/json'
    ) !== 0
) {

    http_response_code(415);

    echo json_encode([
        "success" => false,
        "message" =>
            "JSON content type required"
    ]);

    exit;
}

HTTP status code 415 Unsupported Media Type can be used when the server does not support the media type sent by the client.

24. Content-Type and File Uploads

When a mobile application uploads a file, the request may use multipart form data rather than a JSON request body.


Content-Type:
multipart/form-data

This format can contain text fields and uploaded files in the same request.

25. Content-Type and React Native

React Native applications commonly send JSON data to PHP REST APIs.


const student = {
    name: "Rahul",
    course: "React Native"
};

fetch(
    "https://example.com/api/students",
    {
        method: "POST",

        headers: {
            "Content-Type":
                "application/json"
        },

        body: JSON.stringify(student)
    }
);

26. Common Content-Type Mistakes

  • Using the wrong media type.
  • Forgetting Content-Type when the server expects a specific format.
  • Sending JSON while identifying the body as a different format.
  • Confusing Content-Type with Accept.
  • Sending form data as JSON without the required structure.
  • Assuming every API accepts every content type.
  • Not handling unsupported media types on the server.
  • Forgetting that file uploads commonly use multipart/form-data.

27. Testing Content-Type in Postman

Postman can be used to inspect and test request headers.

For a JSON request, the request can contain:


Content-Type: application/json

Then the Body section can contain:


{
    "name": "Rahul",
    "course": "React Native"
}

28. Complete Content-Type Flow


React Native
      ↓
JavaScript Object
      ↓
JSON.stringify()
      ↓
Content-Type: application/json
      ↓
Request Body
      ↓
PHP REST API
      ↓
Read JSON
      ↓
Validate
      ↓
Process Data
      ↓
Content-Type: application/json
      ↓
JSON Response
      ↓
React Native

29. Complete Content-Type Example

React Native request:


const response = await fetch(
    "https://example.com/api/students",
    {
        method: "POST",

        headers: {
            "Content-Type":
                "application/json"
        },

        body: JSON.stringify({
            name: "Rahul",
            email: "rahul@example.com",
            course: "React Native"
        })
    }
);

PHP response:


header(
    "Content-Type: application/json"
);

echo json_encode([
    "success" => true,
    "message" =>
        "Student created successfully"
]);

30. Complete Content-Type Concept

Content-Type is an essential HTTP header for REST API communication. It tells the receiving system what type of data is contained in the message body.


Request:

POST /api/students

Content-Type: application/json

{
    "name": "Rahul",
    "course": "React Native"
}

For a React Native application communicating with a PHP REST API, application/json will be one of the most frequently used Content-Type values. Other formats such as multipart/form-data are useful when uploading files.

📌 Key Points

  • Content-Type identifies the media type of a message body.
  • application/json is commonly used for JSON REST API data.
  • Content-Type can be used for both requests and responses.
  • Fetch can specify Content-Type inside the headers object.
  • Axios can also send JSON data with an appropriate Content-Type.
  • Postman can be used to test Content-Type headers.
  • PHP can read JSON request bodies using php://input.
  • PHP can return JSON using Content-Type: application/json.
  • text/plain represents plain text.
  • text/html represents HTML content.
  • application/xml represents XML content.
  • application/x-www-form-urlencoded represents encoded form data.
  • multipart/form-data is commonly used for file uploads.
  • Content-Type and Accept have different purposes.
  • HTTP 415 can indicate an unsupported media type.

🧠 Quick Quiz

Question: Which Content-Type is commonly used when sending JSON data to a REST API?