2017-08-15 186 views
0

我正在進行.post API調用,我需要使用multipart/form-data。我知道如何使用JSON進行調用,但我不熟悉multipart/form-data。使用JSON,這是一個超級簡單的調用。只要創建一個類型參數:使用alamofire的multipart/form-data

var parameters:Parameters = [:] 
parameters["username"] = emailTextField.text! 
parameters["password"] = passwordTextField.text! 

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in 
    //Code here 
} 

我們如何寫這個使用表單數據。什麼是最簡單的方法來做到這一點?我不需要上傳任何文件或任何東西。我所能做的就是用極其簡單的物品打電話。使用表單數據做到這一點的最簡潔的方法是什麼?我確信這是一個非常基本的問題,我四處尋找堆棧溢出的幫助,但我只看到它用於更高級的文件調用。我只想知道如何以最簡單的方式做到這一點,因爲它本質上代替了JSON調用。從文檔

+0

見https://github.com/Alamofire/Alamofire#上傳multipart-form-data – nathan

+0

如果我們使用該方法,我們如何放置標題? –

回答

0

例子:

Alamofire.upload(
    multipartFormData: { multipartFormData in 
     multipartFormData.append(unicornImageURL, withName: "unicorn") 
     multipartFormData.append(rainbowImageURL, withName: "rainbow") 
    }, 
    to: "https://httpbin.org/post", 
    encodingCompletion: { encodingResult in 
     switch encodingResult { 
     case .success(let upload, _, _): 
      upload.responseJSON { response in 
       debugPrint(response) 
      } 
     case .failure(let encodingError): 
      print(encodingError) 
     } 
    } 
) 

方法的完整描述(如果你需要設置頁眉Source):

/// Encodes `multipartFormData` using `encodingMemoryThreshold` with the default `SessionManager` and calls 
/// `encodingCompletion` with new `UploadRequest` using the `url`, `method` and `headers`. 
/// 
/// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative 
/// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most 
/// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to 
/// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory 
/// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be 
/// used for larger payloads such as video content. 
/// 
/// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory 
/// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`, 
/// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk 
/// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding 
/// technique was used. 
/// 
/// - parameter multipartFormData:  The closure used to append body parts to the `MultipartFormData`. 
/// - parameter encodingMemoryThreshold: The encoding memory threshold in bytes. 
///          `multipartFormDataEncodingMemoryThreshold` by default. 
/// - parameter url:      The URL. 
/// - parameter method:     The HTTP method. `.post` by default. 
/// - parameter headers:     The HTTP headers. `nil` by default. 
/// - parameter encodingCompletion:  The closure called when the `MultipartFormData` encoding is complete. 
public func upload(
    multipartFormData: @escaping (MultipartFormData) -> Void, 
    usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold, 
    to url: URLConvertible, 
    method: HTTPMethod = .post, 
    headers: HTTPHeaders? = nil, 
    encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?)