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()
}
SWIFT 4:
let url = URL(string: “url”);
var urlRequest = URLRequest(url: url!)
urlRequest.setValue("application/x-www-form-urlencoded",forHTTPHeaderField: "Content-Type")
urlRequest.httpMethod = "POST"
let postString = “paramerter1=value1¶meter2=value2”
urlRequest.httpBody = postString.data(using: .utf8)