2017-08-15 5 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#を参照してください。 uploading-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)?) 
関連する問題