Last active
May 10, 2019 06:47
-
-
Save alfian0/89128374d835aedf8c6f8bc17cf3d913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func generateBoundary() -> String { | |
return "Boundary-\(UUID().uuidString)" | |
} | |
private func createDataBody(withParamaeters parameters: Parameters?, media: [Media]?, boundary: String) -> Data { | |
var body = Data() | |
let linebreak = "\r\n" | |
if let parameters = parameters { | |
for (key, value) in parameters { | |
body.append("--\(boundary + linebreak)") | |
body.append("Content-Disposition: form-data; name=\"\(key)\"\(linebreak + linebreak)") | |
body.append("\(value + linebreak)") | |
} | |
} | |
if let media = media { | |
for photo in media { | |
body.append("--\(boundary + linebreak)") | |
body.append("Content-Disposition: form-data; name=\"\(photo.key)\"; filename=\"\(photo.filename)\"\(linebreak)") | |
body.append("Content-Type: \(photo.mimeType + linebreak + linebreak)") | |
body.append(photo.data) | |
body.append(linebreak) | |
} | |
} | |
body.append("--\(boundary)--\(linebreak)") | |
return body | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Media { | |
let key: String | |
let filename: String | |
let data: Data | |
let mimeType: String | |
init(key: String, filename: String, data: Data, mimeType: String) { | |
self.key = key | |
self.filename = filename | |
self.data = data | |
self.mimeType = mimeType | |
} | |
init?(withImage image: UIImage, forKey key: String) { | |
self.key = key | |
self.mimeType = "image/jpeg" | |
self.filename = "\(arc4random()).jpeg" | |
guard let data = image.jpegData(compressionQuality: 0.7) else { return nil } | |
self.data = data | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urlRequest = URLRequest(url: URL(string: "https://favorestodevapi.azurewebsites.net/v.1/customer/profile/picture")!) | |
let boundary = generateBoundary() | |
let dataBody = createDataBody(withParamaeters: [:], media: [Media(withImage: UIImage(named: "3DA6ADF15C7B3D61615BA5BB726DA6112BC8FFAC.jpg")!, forKey: "image")!], boundary: boundary) | |
let urlSession = URLSession.shared | |
urlRequest.httpMethod = "PUT" | |
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") | |
urlRequest.setValue("Bearer MjQMuRMz1iCXZ84SWpwvwIZSqDrMEDTgaZNS7EfHbbpavBTFPgXfnKaYh5A5tu0pwSb7csfl5P_otCIBjqP6dlQxtix0sdvLthfw71ochCZHdmbiKI9ENIwBcGBK1qIMOWKM-N0L3rwldjTrCwMM_1K8t6aXjR0FjQOTPLPia-doorQ9gwmfMovbBjepzZmVuNK5siKC5ars4IwdP9BH2cRLnqy2_QL9GkMo-6SOQk6LO9sY6rPqKzy8aG7sqBW7fEeRsTFol-KsiVVpE9Q6F_iNFD8Fjv5hy8npkSJNxKRG8O07jMZcnut6Yt6obs97Bdiad6JX3rpPFAHobmR9eLw7D9zYWDR70IFPX-GBStRGaNtI6q1NvfWlTVq-M-BffBZ64fT9VpaYbzlwldsFIq0rHqYu701SbpkiDLd344d--pnoiKZzA575ktgA_FdtBgeql8MDKM92QXi_UV2LTKHUBWfgI8b8vsMuZWNIC3kj0LajZ21JsU6Zm-A72SfWpsaJoLerhw1LWrnP_jSjJ3Lgy-YKGyOIRtLeAkqG1mNz-Drc6i6Tc2ixQ3WDzxJQyqr4Np0-XCxGrMOT2N2zaJEYONzveu3MP0JfAJo5dzyWIEbm2JQ5UH8xJrIu_3vMm0CdHRJA_9yuoqAVoldyuYWLftW7n0_C_XENUb7j5dIwvgOqh2C0iaNGUWV2qOEi", forHTTPHeaderField: "Authorization") | |
urlRequest.httpBody = dataBody | |
urlSession.dataTask(with: urlRequest) { (data, response, error) in | |
} | |
.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
▿ Optional
"Content-Encoding" = gzip;
"Content-Length" = 303;
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 10 May 2019 06:42:21 GMT";
Server = "Microsoft-IIS/10.0";
"Set-Cookie" = "ARRAffinity=92e0f4e5d383bb99e50dd303c95d03b89a4859c6703571f9262e9d743586de39;Path=/;HttpOnly;Domain=favorestodevapi.azurewebsites.net";
Vary = "Accept-Encoding";
"x-powered-by" = "ASP.NET";
} }