Concept

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

Key Idea

Client → sends data → Server → processes → returns response

URLRequest with POST

To perform a POST request, a URLRequest must be configured:

var request = URLRequest(url: url)
request.httpMethod = "POST"

Request Body

POST requests send data in the HTTP body.

Example:

let dataDictionary = [
    "name": "zeeshan",
    "email": "[email protected]",
    "password": "1234"
]

Converting Data to JSON

let requestBody = try JSONSerialization.data(withJSONObject: dataDictionary)
request.httpBody = requestBody

This converts Swift data into JSON format.