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

Mention whether you can use GET request instead of PUT to create a resource?

Câu trả lời

In the context of RESTful API design, using a GET request instead of a PUT request to create a resource is not appropriate. Here are the reasons why:

  1. Purpose and Semantics:

    • GET: The GET method is designed to retrieve data from the server. It is a read-only operation and should not change the state of the resource on the server. GET requests are idempotent and safe, meaning they do not have side effects and can be repeated without altering the resource[9][10].
    • PUT: The PUT method is used to create or update a resource at a specified URI. It is idempotent, meaning multiple identical PUT requests will result in the same state of the resource. PUT can create a new resource if it does not exist or update an existing resource entirely if it does[1][2][3][4].
  2. Idempotency and Safety:

    • GET: GET requests are idempotent and safe, meaning they do not modify the resource and can be repeated without any side effects[9][10].
    • PUT: PUT requests are idempotent but not safe, as they modify the resource on the server. Repeating a PUT request will result in the same state of the resource, but it does change the resource[1][2][3][4].
  3. HTTP Specifications and Best Practices:

    • According to HTTP specifications and best practices, GET should only be used for retrieving data, while PUT should be used for creating or updating resources. Using GET to create a resource would violate these conventions and could lead to unexpected behavior and security issues[1][2][3][4][6].
  4. Cacheability:

    • GET: Responses to GET requests are cacheable, which means they can be stored and reused without sending a new request to the server. This is beneficial for performance but inappropriate for operations that modify resources[9][10].
    • PUT: Responses to PUT requests are generally not cacheable, as they modify the resou...
junior

junior

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

senior

What do you mean by idempotent operation?

junior

What are the core components of a HTTP Request?

middle

What is Payload?

Bình luận

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

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