A POST request is used to send data to a server.
It is typically used when creating new resources, such as:
user registration
form submission
creating records in a database
Client → sends data → Server → processes → returns response
To perform a POST request, a URLRequest must be configured:
var request = URLRequest(url: url)
request.httpMethod = "POST"
POST requests send data in the HTTP body.
Example:
let dataDictionary = [
"name": "zeeshan",
"email": "[email protected]",
"password": "1234"
]
let requestBody = try JSONSerialization.data(withJSONObject: dataDictionary)
request.httpBody = requestBody
This converts Swift data into JSON format.