Sobes.tech
Junior — Middle

How do you differentiate between various HTTP requests and what criteria are used for classification?

sobes.tech AI

Answer from AI

HTTP requests are distinguished by several criteria, the main one being the request method. Methods define the action the client wants to perform on the resource on the server. Main HTTP methods:

  • GET — request to retrieve data.
  • POST — send data to the server to create a resource.
  • PUT — update or replace a resource.
  • DELETE — delete a resource.
  • HEAD — get only response headers.

Besides the method, requests are classified by:

  • URL (resource address) — specifies which resource the request targets.
  • Headers — contain meta-information, e.g., content type, authorization.
  • Request body — data sent, e.g., in POST or PUT.

In Go (Golang), the r.Method field of http.Request is used to distinguish requests.

Example handling different methods in Go:

func handler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "GET":
        // handle GET
    case "POST":
        // handle POST
    default:
        http.Error(w, "Method not supported", http.StatusMethodNotAllowed)
    }
}
How do you differentiate between various HTTP… - sobes.tech