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:
    Accept: <MIME_type>/<MIME_subtype>
    Accept: <MIME_type>/*
    Accept: */*
  • Examples:
    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:
    Content-Type: <media-type>
  • Examples:
    Content-Type: te...
middle

middle

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

middle

What's the difference between REST & RESTful?

middle

What are the best practices to create a standard URI for a web service?

expert

Enlist some important constraints for RESTful web services

Bình luận

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

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