Câu hỏi phỏng vấn API Design
Câu hỏi

What is the use of Accept and Content-Type Headers in HTTP Request?

Câu trả lời

The Accept and Content-Type headers in an HTTP request play crucial roles in content negotiation and data handling between the client and server. Here’s a detailed explanation of their uses:

Accept Header

The Accept header is a request header used by the client to inform the server about the types of media that the client can process. This header helps in content negotiation, allowing the server to select an appropriate response format that the client can understand.

Key Points:

  • Purpose: Indicates the media types that are acceptable for the response.
  • Syntax:
    http Copy
    Accept: <MIME_type>/<MIME_subtype>
    Accept: <MIME_type>/*
    Accept: */*
  • Examples:
    http Copy
    Accept: text/html
    Accept: image/*
    Accept: */*
    Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
  • Directives:
    • <MIME_type>/<MIME_subtype>: A specific MIME type, e.g., text/html.
    • <MIME_type>/*: Any subtype of a specific type, e.g., image/*.
    • */*: Any MIME type.
    • ;q=: Quality factor weighting to indicate preference.

If the Accept header is not present, the server assumes that the client can accept any media type[1][2].

Content-Type Header

The Content-Type header is used to indicate the media type of the resource being sent to the server or the client. This header is crucial for the server to correctly interpret the data in the request body and for the client to process the response body.

Key Points:

  • Purpose: Specifies the media type of the resource or data being sent.
  • Syntax:
    http Copy
    Content-Type: <media-type>
  • Examples:
    http Copy
    Content-Type: te...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What are disadvantages of REST web services?

middle

What are the primary security issues of web service?

middle

What's the difference between REST & RESTful?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào