Generated Swift code at Insomnia works for 1 key and 1 value, but always give an error for 2+ keys, values. How to send an array of arrays as a POST method in Swift4+?
Insomnia code:
let headers = [
"content-type": "multipart/form-data; boundary=---011000010111000001101001",
"accept": "application/json"
]
let parameters = [
[
"name": "email",
"value": "example@gmail.com"
],
[
"name": "token",
"value": "911"
]
]
let boundary = "---011000010111000001101001"
var body = ""
let error: NSError? = nil
do{
for param in parameters {
let paramName = param["name"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
let contentType = param["content-type"]!
let fileContent = try String(contentsOfFile: filename, encoding: String.Encoding.utf8)
if (error != nil) {
print(error as Any)
}
body += "; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(contentType)\r\n\r\n"
body += fileContent
} else if let paramValue = param["value"] {
body += "\r\n\r\n\(paramValue)"
}
}
} catch { print(error.localizedDescription) }
let request = NSMutableURLRequest(url: NSURL(string: "https://server.../verify")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
let postData = body.data(using: .utf8)
request.httpBody = postData!
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error as Any)
} else {
let httpResponse = response as? HTTPURLResponse
print("response: \(httpResponse!)")
let str = String(data: data!, encoding: .windowsCP1250)
print(str!)
}
})
dataTask.resume()
It seems like no values at all are transferred to the server, however, for 1 value there is no problem at all:
let parameters = [
[
"name": "email",
"value": "example@gmail.com"
]
]
For Swift4, Swift5 POST usage better to make the request with:
components.queryItems = [
URLQueryItem(name: "email", value: "example@gmail.com"),
URLQueryItem(name: "token", value: "3909")
]
The server is ok, the syntax also, however, it depends on the version of Swift as I see.
The whole code:
let url = URL(string: "https://server.../verify")!
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems = [
URLQueryItem(name: "email", value: "example@gmail.com"),
URLQueryItem(name: "token", value: "3909")
]
let query = components.url!.query
var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = Data(query!.utf8)
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error!)
} else {
let httpResponse = response as? HTTPURLResponse
print("response: \(httpResponse!)")
let str = String(data: data!, encoding: .utf8)
print(str!)
}
})
dataTask.resume()