How do I attach multiple data with a POST request in Swift.
As attached screen shot from Postman it is working fine when select the x-www-form-urlencoded
option
How do I attach 5 data with the body like 'x-www-form-urlencoded'
option?
Here the code:
var request = URLRequest(url: urlString)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded;charset=UTF-8", forHTTPHeaderField: "Content-Type")
var urlComponents = URLComponents()
urlComponents.queryItems = [
URLQueryItem(name: “***”, value: “***”),
URLQueryItem(name: "***", value: "***"),
URLQueryItem(name: "***", value: "***"),
URLQueryItem(name: "***", value: "***"),
URLQueryItem(name: "***", value: "***"),
]
request.httpBody = urlComponents.percentEncodedQuery?.data(using: String.Encoding.utf8)
let loadDataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let _ = error{
completion(false,error)
}
else if let response = response as? HTTPURLResponse{
if response.statusCode != 200{
completion(false,error)
}
else{
do{
if let parsedData = try? JSONSerialization.jsonObject(with: data!, options: []){
let ff = parsedData as? Dictionary<String,Any>
print(ff)
}
}
}
}
}//let loadDataTask
loadDataTask.resume()
}
Code
in postman (attached screen). Select Swift in the modal appears after clicking Code. You will get the code you need, remove the unnecessary postman parameters