Sobes.tech
Junior — Middle

Can you list the main types of HTTP requests used when interacting with a server?

sobes.tech AI

Answer from AI

Main types of HTTP requests (methods) used when interacting between client and server:

  • GET — request to retrieve data from the server. Does not change the state.
  • POST — send data to the server, usually to create new resources.
  • PUT — update or replace a resource on the server.
  • PATCH — partial update of a resource.
  • DELETE — delete a resource.
  • HEAD — request headers of a resource without the body.
  • OPTIONS — request supported methods for a resource by the server.

Example of usage in iOS with URLSession:

var request = URLRequest(url: URL(string: "https://example.com/api/item")!)
request.httpMethod = "POST"
request.httpBody = ... // data to send

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    // handle response
}
task.resume()