Skip to content

Instantly share code, notes, and snippets.

@LinkonBSMRSTU
Forked from DejanEnspyra/multipart.swift
Created April 8, 2021 04:02
Show Gist options
  • Select an option

  • Save LinkonBSMRSTU/4722febe89b71db237dc7feaabcefad0 to your computer and use it in GitHub Desktop.

Select an option

Save LinkonBSMRSTU/4722febe89b71db237dc7feaabcefad0 to your computer and use it in GitHub Desktop.

Revisions

  1. @DejanEnspyra DejanEnspyra created this gist Jul 2, 2017.
    35 changes: 35 additions & 0 deletions multipart.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    func requestWith(endUrl: String, imageData: Data?, parameters: [String : Any], onCompletion: ((JSON?) -> Void)? = nil, onError: ((Error?) -> Void)? = nil){

    let url = "http://google.com" /* your API url */

    let headers: HTTPHeaders = [
    /* "Authorization": "your_access_token", in case you need authorization header */
    "Content-type": "multipart/form-data"
    ]

    Alamofire.upload(multipartFormData: { (multipartFormData) in
    for (key, value) in parameters {
    multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
    }

    if let data = imageData{
    multipartFormData.append(data, withName: "image", fileName: "image.png", mimeType: "image/png")
    }

    }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
    switch result{
    case .success(let upload, _, _):
    upload.responseJSON { response in
    print("Succesfully uploaded")
    if let err = response.error{
    onError?(err)
    return
    }
    onCompletion?(nil)
    }
    case .failure(let error):
    print("Error in upload: \(error.localizedDescription)")
    onError?(error)
    }
    }
    }