Home > Backend Development > PHP Tutorial > What is the role of Content-Type?

What is the role of Content-Type?

藏色散人
Release: 2023-04-05 19:48:02
Original
5166 people have browsed it

Content-Type The entity header is used to indicate the MIME type media type of the resource.

In the response, the Content-Type header tells the client the content type of the actual content returned. Browsers perform MIME lookups in some cases and do not necessarily honor the value of this header; to prevent this behavior, the header X-Content-Type-Options can be set to nosniff.

In the request (such as POST or PUT), the client tells the server the type of data actually sent.

Syntax:

Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something
Copy after login

Directions:

media-type
Copy after login

The MIME type of the resource or data.

charset
Copy after login

Character encoding standard.

boundary
Copy after login

A boundary is required for multipart entities, which consists of 1 to 70 characters from a set of characters known to be very robust through email gateways, rather than ending in whitespace. It is used to encapsulate the boundaries of multiple parts of a message.

Example

##Content-Type in HTML form

In the POST request generated by submitting the HTML form, request The Content-Type of the header is specified by the enctype attribute on the
element

<form action="/" method="post" enctype="multipart/form-data">
  <input type="text" name="description" value="some text">
  <input type="file" name="myFile">
  <button type="submit">Submit</button></form>
Copy after login

The request header looks like this (some headers are omitted here):

POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575

---------------------------974767299852498929531610575
Content-Disposition: form-data; name="description" 

some text
---------------------------974767299852498929531610575
Content-Disposition: form-data; name="myFile"; filename="foo.txt" 
Content-Type: text/plain 

(content of the uploaded file foo.txt)
---------------------------974767299852498929531610575
Copy after login

Browser compatibility

What is the role of Content-Type?

The above is the detailed content of What is the role of Content-Type?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template