Download & Upload zip file - ios

I am trying to mimic the following curl:
curl -v -F file=#/Users/myuser/Downloads/shelly-homekit-Shelly25.zip http://10.0.1.7/update
to use the curl command I downloaded the zip file and saved it to my computer.
My app should download the zip file, store it to the device and upload it to the server.
I tried both uploading it as a file and as Data with no success:
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
debugPrint(response)
if response.error == nil, let zipPath = response.fileURL?.path {
let url = URL(string: zipPath)!
let headers: HTTPHeaders = [
.contentType("multipart/form-data")
]
if let data = try? Data(contentsOf: url) {
AF.upload(data, to: "http://"+dev.ipAddress+"/update",headers: headers).responseDecodable(of: HTTPBinResponse.self) { response in
debugPrint(response)
}
}
}
}
I get the following error:
Thank you for your help

Like mentioned in the comments you meed to allow arbitrary loads. See Transport security has blocked a cleartext HTTP. For more details

This didn't help:
Like mentioned in the comments you meed to allow arbitrary loads. See Transport security has blocked a cleartext HTTP. For more details
I set the permission as mentioned in the link:
Permission
But getting the following error:
Error
I also made some changes to my code:
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
//debugPrint(response)
if response.error == nil {
AF.upload(response.fileURL!, to: "http://"+dev.ipAddress+"/update").responseDecodable(of: HTTPBinResponse.self) { response_up in
debugPrint(response_up)
}
}
}

I used Wireshark to sniff the request sent by the curl command:
POST /update HTTP/1.1
Host: 10.0.1.10
User-Agent: curl/7.64.1
Accept: */*
Content-Length: 988427
Content-Type: multipart/form-data; boundary=------------------------c67b500e63a02f3d
Expect: 100-continue

--------------------------c67b500e63a02f3d
Content-Disposition: form-data; name="file"; filename="shelly-homekit-Shelly25.zip"
Content-Type: application/octet-stream
Not sure how to translate it to AF code and add the fileData to the request, it should be a POST request and I believe the following code create the right http body:
let boundary = "Boundary-\(UUID().uuidString)"
let headers: HTTPHeaders = [
.contentType("multipart/form-data; boundary=\(boundary)")
]
let parameters = [
[
"key": "file",
"value": "shelly-homekit-Shelly25.zip",
"type": "application/octet-stream",
"src":response.fileURL!.path
]] as [[String : Any]]
var body = ""
for param in parameters {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
body += "\r\nContent-Type: \(param["contentType"] as! String)"
//let paramSrc = param["src"] as! String
let paramValue = param["value"] as! String
let paramType = param["type"] as! String
//let fileData = try? NSData(contentsOfFile:paramSrc, options:[]) as Data
body += "; filename=\"\(paramValue)\"\r\n" + "Content-Type: " + paramType
}
let postData = body.data(using: .utf8)

The following code works:
let data = try? NSData(contentsOf: URL(string:str)!) as Data
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data!,withName: "file",fileName: file_name ,mimeType: "application/octet-stream")
}, to: "http://"+dev.ipAddress+"/update")
.response { response in
if response.response?.statusCode != 200 {
devicesUpdateError.append(dev.name)
}
}

Related

How to implement the Bearer Token to validate the API url

I set up the API and all, the only thing is Bearer Token I couldn't find any information about any code on how to implement it so it can validate the URL I am using as API.
do I need to create new swift file just for bearer token or I can write the code to the API swift file "the code below is api file"
static let shared = APICaller()
private let baseURL = "http://000.000.000.000:3030/api/"
private init() {}
var vehicles = [Vehicles]()
func getVehicles(for id: String, IMEI: Int, completed: #escaping (Result<[Vehicles],Errors>) -> Void ){
let endpoint = baseURL + "GetVehicle/?UserIdentificationValue=346HIU4623UIHG3I3I&IMEI=216216123612"
guard let url = URL(string: endpoint) else {
completed(.failure(.invalidURL))
return
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let _ = error {
completed(.failure(.unableToComplete))
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
completed(.failure(.invalidResponse))
return
}
guard let data = data else {
completed(.failure(.invalidData))
return
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
self.vehicles = try JSONDecoder().decode([Vehicles].self, from: data)
DispatchQueue.main.async {
completed(.failure(.invalidData))
}
} catch {
completed(.failure(.invalidData))
}
}
task.resume()
Thanks In Advance
Also I am new to swift so I would appreciate if you can tell me my API code is correct or needs any fixes since its about receiving some car info and putting into a table view cell :)
I have attached the request including headers in which you need to pass Bearer token like did in below code
let headers = [
"content-type": "application/json",
"authorizetoken": "NjQzOPA2N0NDNDFAH4CNDk3R23F2FQUY0NjV3FFE=",
"cache-control": "no-cache",
]
let parameters = ["id":"123456789"] as [String : Any]
let postData = try? JSONSerialization.data(withJSONObject: parameters, options: [])
let request = NSMutableURLRequest(url: NSURL(string: "Your URL")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 120.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as? Data
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData

send multi part form data in swift

I used this code below to send multipart params
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer \(myToken)",
"cache-control": "no-cache"
]
let parameters = [
[
"name": "firstname",
"value": "alex"
],
[
"name": "lastname",
"value": "black"
],
[
"name": "birthdate_day",
"value": "1"
],
[
"name": "birthdate_month",
"value": "5"
],
[
"name": "birthdate_year",
"value": "1989"
],
[
"name": "gender",
"value": "m"
],
[
"name": "avatar",
"fileName": "\(imageURL)"
]
]
let boundary = "Boundary-\(NSUUID().uuidString)"
var body = ""
let error: NSError? = nil
for param in parameters {
let paramName = param["name"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
if let contentType = param["content-type"] {
do {
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
} catch {
print(error)
}
}
} else if let paramValue = param["value"] {
body += "\r\n\r\n\(paramValue)"
}
}
let postData = NSMutableData(data: body.data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "myUrl")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
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(httpResponse?.statusCode as Any)
}
})
dataTask.resume()
return dataTask
the image url and the rest of the data But I will receive Satus code 500 I know that this error is server side But the android version is using the same api url and that's working well I know that this code can be fix and maybe small changes can fix this code for working
use URL instead of NSURL
var request = URLRequest is mutable, use this instead of NSMutableURLRequest
var data = Data() is mutable, use this instead of NSMutableData
append the file blob data safely using Data(contentsOf:options:) method
content-type is missing in parameters, so if let contentType = param["content-type"] { ... } will fail to proceed, using application/octet-stream default mime type
depending on the server, it might be necessary to provide a filename for the uploads
I fixed all above concerns and moved the URLRequest.httpBody generating code to following extension.
extension URLRequest {
private func formHeader(_ name: String, crlf: String, fileName: String? = nil, mimeType: String? = nil) -> String {
var str = "\(crlf)Content-Disposition: form-data; name=\"\(name)\""
guard fileName != nil || mimeType != nil else { return str + crlf + crlf }
if let name = fileName {
str += "; filename=\"\(name)\""
}
str += crlf
if let type = mimeType {
str += "Content-Type: \(type)\(crlf)"
}
return str + crlf
}
private func getFileUrl(_ file: Any) -> URL? {
if let url = file as? String {
return URL(string: url)
}
return file as? URL
}
private func getFileData(_ url: URL) -> Data? {
do {
return try Data(contentsOf: url, options: .mappedIfSafe)
} catch {
print(error)
return nil
}
}
mutating func setPost(body parameters: [[String: Any]]) {
let boundary = "Boundary+\(arc4random())\(arc4random())"
self.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var data = Data()
data.append("--\(boundary)".data(using: .utf8)!)
let crlf = "\r\n"
for parameter in parameters {
guard let paramName = parameter["name"] as? String else { continue }
if let value = parameter["value"] {
let header = formHeader(paramName, crlf: crlf)
data.append("\(header)\(value)".data(using: .utf8)!)
} else if let file = parameter["file"], let fileUrl = getFileUrl(file), let fileData = getFileData(fileUrl) {
let fileName = parameter["fileName"] as? String
let contentType = parameter["content-type"] as? String
let header = formHeader(paramName, crlf: crlf, fileName: fileName ?? fileUrl.lastPathComponent, mimeType: contentType ?? "application/octet-stream")
data.append(header.data(using: .utf8)!)
data.append(fileData)
} else {
print("\(paramName): empty or invalid value")
continue
}
data.append("\(crlf)--\(boundary)".data(using: .utf8)!)
}
data.append("--\(crlf)".data(using: .utf8)!)
self.httpBody = data
self.httpMethod = "POST"
}
}
Usage
let parameters = [
["name": "firstname", "value": "alex"],
["name": "avatar", "file": URL],
["name": "avatar", "file": "file:///", "fileName": "image.png", "content-type": "image/png"]
]
request.setPost(body: parameters)
Note above in parameters
file key represents either a URL object or file path String.
fileName: image.png is for backend, represents name of the file.
Finally add headers and create URLSession.shared.dataTask as your original code.
Update-2 function instead of an extension
func getParameterData(_ name: String, parameter: [String : Any]) -> Data? {
var str = "\r\nContent-Disposition: form-data; name=\"\(name)\""
if let value = parameter["value"] {
return "\(str)\r\n\r\n\(value)".data(using: .utf8)!
}
guard
let file = parameter["file"],
let url = (file is String ? URL(string: file as! String) : file as? URL)
else {
return nil
}
let data: Data
do {
data = try Data(contentsOf: url, options: .mappedIfSafe)
} catch {
print(error)
return nil
}
let fileName = (parameter["fileName"] as? String) ?? url.lastPathComponent
str += "; filename=\"\(fileName)\"\r\n"
let contentType = (parameter["content-type"] as? String) ?? "application/octet-stream"
str += "Content-Type: \(contentType)\r\n"
return (str + "\r\n").data(using: .utf8)! + data
}
func setPostRequestBody(_ request: inout URLRequest, parameters: [[String: Any]]) {
let boundary = "Boundary+\(arc4random())\(arc4random())"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
var data = Data()
data.append("--\(boundary)".data(using: .utf8)!)
for parameter in parameters {
guard
let name = parameter["name"] as? String,
let value = getParameterData(name, parameter: parameter)
else {
continue
}
data.append(value)
data.append("\r\n--\(boundary)".data(using: .utf8)!)
}
data.append("--\r\n".data(using: .utf8)!)
request.httpBody = data
}
Usage-2
var request = URLRequest(url: URL(string: "myUrl")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
setPostRequestBody(&request, parameters: [
["name": "firstname", "value": "alex"],
["name": "avatar", "file": URL object or path String]
])
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
guard error != nil else {
print(error!.localizedDescription)
return
}
let statusCocde = (response as? HTTPURLResponse)?.statusCode
print(statusCode ?? 0)
if let data = data {
print(String(data: data, encoding: .utf8) ?? "")
}
}
dataTask.resume()

No HTTP resource was found that matches the request URI web api using ASP.NET for ios Swift

I need to send POST request to the server and these are the parameters
let params : NSDictionary = ["userid":"\(userID)","sessionid":"\(sessionID)","site_id": "\(siteIDParam)", "latitude":"\(currentlatitude)", "longitude":"\(currentlongitude)", "action": "1", "comments":commentTextView.text ?? ""]
But the problem is I need to send image as byte array and i am using the following code (Swift 3)
func uploadImageRequest(image: UIImage? , urlString: String, imageName : String ,param: [String: AnyObject]? , completion:#escaping(_ success:Bool , _ object : AnyObject?) -> ())
{
let url = NSURL(string: urlString)
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (image == nil)
{
return
}
let image_data = UIImageJPEGRepresentation(image!, 0.5)
if(image_data == nil)
{
return
}
let body = NSMutableData()
let fname = "photo.png"
let mimetype = "image/*"
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"\(imageName)\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
if param != nil {
for (key, value) in param! {
body.append("--\(boundary)\r\n")
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.append("\(value)\r\n")
}
}
request.httpBody = body as Data
_ = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
if let data = data {
// Json Response
let json = try? JSONSerialization.jsonObject(with: data, options: [])
// response.
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
completion(true, json as AnyObject?)
} else {
completion(false, json as AnyObject?)
}
} else {
completion(false, error?.localizedDescription as AnyObject?)
}
}.resume()
}
The image is not converted properly I guess so that the response get failed when triggered and I am getting the following error
Message = "No HTTP resource was found that matches the request URI
This API works fine in Android device but it cant be achievied in iOS. I have searched all the links and I couldnt get a proper solution for this. I have also tried using Alamofire but still I got the same error.
You have to set the NSAllowsArbitraryLoads key to YES under NSAppTransportSecurity dictionary in your .plist file.
You can do this easily using alamofire, use the following code to do these things done.
let multipartHeaders = ["Content-Type":"multipart/form-data"]
let paramString: String = "parameters in json string..."
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(paramString.data(using: .utf8)!, withName: "json")
multipartFormData.append(<imageDataHere>, withName: "myimage", fileName: "myImage.jpeg", mimeType: "image
jpeg")
}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: url, method: .post, headers:
multipartHeader) { (encodingResult) in
switch encodingResult {
case .success(let request, _, _):
request.responseJSON(completionHandler: { (response) in
print(response)
})
case .failure(let error):
print(error.localizedDescription)
}
}
This piece of code helped me solve my problem. I just send my image as a file and remaining parameters as a string without appending it to my multipart body.
let imageData = UIImageJPEGRepresentation(userImage, 0.2)!
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let imageURL = docDir.appendingPathComponent("tmp.jpg")
try! imageData.write(to: imageURL)
let multipartHeaders = ["Content-Type":"multipart/form-data"]
let paramString: String = "userid=\(userId)&action=1"
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageURL, withName: "profilepic")
}, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: URL(string: ATTENDANCE_URL+paramString)!, method: .post, headers:
multipartHeaders) { (encodingResult) in
switch encodingResult {
case .success(let request, _, _):
request.responseJSON(completionHandler: { (response) in
print(response.result.value)
let value = response.result.value as! [String: Any]
})
case .failure(let error):
print(error.localizedDescription)
}
}

Send parameters AND data to web server with Swift 3

I'm trying to figure out how to send a photo from an iPhone to my web server.
I also need to send parameters containing the size of the photo, it's filename and other additional information about the photo in the same request as the parameter data.
The code below is on the right track I think, but where do I put the parameter data called params:
let params: Array<String> = [aI.filename, String(aI.size), String(aI.dateTime.year), String(aI.dateTime.month), String(aI.dateTime.day), String(aI.dateTime.hour), String(aI.dateTime.minute), String(aI.dateTime.second), String(aI.dateTime.millisecond)]
var serverURL = URL(string: "http://192.168.0.23/upload.php");
var req = NSMutableURLRequest(url: serverURL!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 60.0);
//Set request to post
req.httpMethod = "POST";
//Set content type
req.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type");
let task = URLSession.sharedSession().dataTaskWithRequest(req){ data, response, error in
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
}
task.resume()
return task
Allthough some of the answers pushed me in the right direction, they still didn't fit my project and so I continued googling an I managed to find exactly what I needed in the following article: http://swiftdeveloperblog.com/image-upload-example/
I needed to make the HTTP request asynchronously and using sessions,
which I didn't specify in the question because the question was merely about how to send both several parameters along with data in one single request.
It is called Multipart Form Data when doing so.
I had to modify the code from the article a little bit to make it work for my application,
so I'm sharing my Swift 3 code below:
Trigger code
let params = [
"filename" : chunkOwner.filename ,
"size" : String(describing: chunkOwner.size) ,
"year" : String(chunkOwner.dateTime.year) ,
"month" : String(chunkOwner.dateTime.month) ,
"day" : String(chunkOwner.dateTime.day) ,
"hour" : String(chunkOwner.dateTime.hour) ,
"minute" : String(chunkOwner.dateTime.minute) ,
"second" : String(chunkOwner.dateTime.second) ,
"millisecond" : String(chunkOwner.dateTime.millisecond) ,
]
uploadChunk(url: URL(string: "http://192.168.0.23/upload.php")!, data: photoData, params: params)
Upload code:
func uploadData(url: URL, data: Data!, params: [String: String])
{
let cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData;
let request = NSMutableURLRequest(url: url, cachePolicy: cachePolicy, timeoutInterval: 6.0);
request.httpMethod = "POST";
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if(data == nil) { return; }
request.httpBody = createBodyWithParameters(parameters: params, filePathKey: "file", data: data, boundary: boundary)
//myActivityIndicator.startAnimating();
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print(json)
}catch
{
//if you recieve an error saying that the data could not be uploaded,
//make sure that the upload size is set to something higher than the size
print(error)
}
}
task.resume()
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, data: Data!, boundary: String) -> Data {
var body = Data();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
let mimetype = "text/csv"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(parameters!["filename"]!)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
Also include the following code at the bottom of your .swift file outside of your class:
extension Data {
mutating func appendString(string: String) {
append(string.data(using: .utf8)!)
}
}
And for the PHP upload script I did some changes and now looks like this:
<?php
$target_dir = "/var/www/html/uploads";if(!file_exists($target_dir)){
mkdir($target_dir, 0777, true);
}
$target_dir = $target_dir . "/" . basename($_FILES["file"]["name"]);
echo count("size: ".$_FILES["file"]["tmp_name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)){
echo json_encode([
"Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.",
"Status" => "OK",
]);
} else {
echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
]);
}
?>
Important Note:
Your app will fail to upload data if your server php file called
php.ini is configured to accept files smaller than the data you're
trying to upload.
For example: If php.ini is configured to accept 2 MB, then any
uploads larger than 2 MB will be ignored and your app will receive a
response saying that something went wrong.
To change the file size acceptance in php.ini you need to look for
the variable called upload_max_filesize and post_max_sizeand change those to whatever file size
your system requires.
You can put them to httpBody or to httpBodyStream (by using NSInputStream)
But don't forget to transform params for server protocol (for example xml, json, or binary data with custom format).
For your content type (application/x-www-form-urlencoded), you can find format in wikipedia:
keyName=value&keyName2=value2
The keys and values should contain of URLPathAllowedCharacterSet, to achieve it you can use stringByAddingPercentEncodingWithAllowedCharacters.
To convert the KeyValue string to NSData, you can use method dataUsingEncoding.
I am sharing you one way of posting data using NSURLConnection in Swift3
Your URL
var serverURL = URL(string: "http://192.168.0.23/upload.php")
Your parameters to be like this , just discuss with server people to which parameters you to need pass data Then assign your value to that parameter like below
serverparameter1 = \(value to post)& serverparameter2 = \(value to post2).......
With your params I did like this have a look
let params = "filename= \(aI.filename)&size = \(String(aI.size))& dateTimeYear =\(String(aI.dateTime.year))&dateTimeMonth =\(String(aI.dateTime.month))& dateTimeDay =\(String(aI.dateTime.day))&dateTimeHour =\(String(aI.dateTime.hour))&dateTimeMinute =\(String(aI.dateTime.minute))&dateTimeSecond =\(String(aI.dateTime.second))&dateTimeMilliSecond=\(String(aI.dateTime.millisecond))"
Convert your Photo Data to Base64String like below
var base64String: NSString!
let myImage = UIImage(named:"image.png")
let imageData = UIImageJPEGRepresentation(myImage, 0.9)
base64String = imageData!.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithLineFeed) as NSString!
print(base64String)
then pass as stringParameter
&ImageDataStr = \(base64String)
then final Url seems to be look like
\(serverURL)/\(params)
OR
\(serverURL)/Upload?\(params)
Step by step request
var serverURL = URL(string: "http://192.168.0.23/upload.php")
let params = "filename= \(aI.filename)&size = \(String(aI.size))& dateTimeYear =\(String(aI.dateTime.year))&dateTimeMonth =\(String(aI.dateTime.month))& dateTimeDay =\(String(aI.dateTime.day))&dateTimeHour =\(String(aI.dateTime.hour))&dateTimeMinute =\(String(aI.dateTime.minute))&dateTimeSecond =\(String(aI.dateTime.second))&dateTimeMilliSecond=\(String(aI.dateTime.millisecond))&photoDataStr = \(base64String)"
var status:NSString = "\(serverURL)/Upload?\(params)" as NSString
status = status.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
let url = URL(string: status as String)!
let request = URLRequest(url: url, cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 600)
// need synchronous
Here you will get responseData
var response:URLResponse?
var responseD:Data = try! NSURLConnection.sendSynchronousRequest(request, returning:&response)
Finally make that BinaryData to readable
// save to string - the result came from the Server call
var serverResults:NSString = NSString(data: responseD, encoding: String.Encoding.utf8.rawValue)!
print(serverResults)
For Example your Result
if serverResults.range(of: "RESULT>APPROVED").location != NSNotFound
{
return "Data posted"
}
else
{
return "Failed to post"
}

Alamofire get pdf as NSData

I use this alamofire request to get a pdf file, i want to save it as NSData:
func makeDataCall(urlString: String, completionHandler: (responseObject: NSData?, error: NSError?) -> ()) {
//Perform request
Alamofire.request(.GET, urlString, headers: ["Authorization": auth])
.responseData { request, response, responseData in
print(request)
print(response)
print(responseData)
completionHandler(responseObject: responseData.data, error: nil)
}
}
In the response i get this:
"Content-Length" = 592783;
"Content-Type" = "application/pdf";
However responseData.data is nil.
What am i doing wrong?
Editing my previous response, I read your question too quickly.
To download a file like a pdf you should use Alamofire.download rather than request.
There's a section on it in the docs:
https://github.com/Alamofire/Alamofire#downloading-a-file
just checked with some random pdf from the internet and this works for me just fine:
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)
Alamofire.download(.GET, "http://box2d.org/manual.pdf", destination: destination)
.response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
}
}
In Swift 4 use this code if you want to download a pdf
let h: HTTPHeaders = [
"Accept": "application/pdf",
"Content-Type": "application/pdf",
]
//random document name
let randomString = NSUUID().uuidString
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent("\(randomString).pdf")
return (documentsURL, [.removePreviousFile])
}
Alamofire.download("yourURL",method: .get,headers: h, to: destination).response { response in
if let destinationUrl = response.destinationURL {
print("destinationUrl \(destinationUrl.absoluteURL)")
}
}

Resources