Component - POST request with JSON body
This UI component demonstrates how to send a POST request with a JSON body in an iOS Swift app. It includes a button that, when tapped, sends data to a server and shows the response status on screen.
This UI component demonstrates how to send a POST request with a JSON body in an iOS Swift app. It includes a button that, when tapped, sends data to a server and shows the response status on screen.
UIViewController ├── UIButton (Send POST Request) └── UILabel (Response Status)
let url = URL(string: "https://example.com/api")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let jsonBody = ["name": "Alice", "age": 30] as [String : Any]
request.httpBody = try? JSONSerialization.data(withJSONObject: jsonBody)
print(String(data: request.httpBody!, encoding: .utf8)!)var request = URLRequest(url: URL(string: "https://api.example.com")!) request.httpMethod = "POST" let json = ["key": "value"] request.httpBody = try? JSONSerialization.jsonObject(with: json)
Pass the test and this pattern moves unknown