Sobes.tech
Junior — Middle

What methods can be used to send data within an HTTP request?

sobes.tech AI

Answer from AI

In the context of an HTTP request, data can be sent using the following methods:

  • GET — data is transmitted in the URL as query parameters. Usually used for retrieving information.
  • POST — data is sent in the request body, often in JSON, XML, or form-data format. Used for creating resources.
  • PUT — sending data in the request body to update an existing resource.
  • PATCH — partial update of a resource, with data also in the request body.
  • DELETE — usually does not contain a body, but may include data to specify the deletion.

In iOS, data in an HTTP request is often sent using URLRequest, where you can specify the method and body of the request, for example:

var request = URLRequest(url: URL(string: "https://api.example.com/data")!)
request.httpMethod = "POST"
let json = ["key": "value"]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")