You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.5 KiB
59 lines
2.5 KiB
//
|
|
// Created by Marco Schmickler on 22.03.15.
|
|
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
|
|
func urlRequestWithComponents(urlString: String, parameters: NSDictionary) -> (URLRequestConvertible, NSData) {
|
|
|
|
// create url request to send
|
|
var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
|
|
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
|
|
//let boundaryConstant = "myRandomBoundary12345"
|
|
let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"
|
|
let contentType = "multipart/form-data;boundary=" + boundaryConstant
|
|
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
|
|
|
|
|
|
// create upload data to send
|
|
let uploadData = NSMutableData()
|
|
|
|
// add parameters
|
|
for (key, value) in parameters {
|
|
|
|
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
|
|
|
|
if value is NetData {
|
|
// add image
|
|
var postData = value as! NetData
|
|
|
|
//uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
|
|
|
|
// append content disposition
|
|
var filenameClause = " filename=\"\(postData.filename)\""
|
|
let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"
|
|
let contentDispositionData = contentDispositionString.dataUsingEncoding(NSUTF8StringEncoding)
|
|
uploadData.appendData(contentDispositionData!)
|
|
|
|
|
|
// append content type
|
|
//uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) // mark this.
|
|
let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"
|
|
let contentTypeData = contentTypeString.dataUsingEncoding(NSUTF8StringEncoding)
|
|
uploadData.appendData(contentTypeData!)
|
|
uploadData.appendData(postData.data)
|
|
|
|
} else {
|
|
uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
|
|
}
|
|
}
|
|
uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
|
|
|
|
// return URLRequestConvertible and NSData
|
|
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
|
|
}
|
|
|
|
class NetworkHelper {
|
|
}
|