gpt4 book ai didi

ios - 使用 alamofire 的多部分/表单数据

转载 作者:行者123 更新时间:2023-11-30 12:13:46 27 4
gpt4 key购买 nike

我正在进行 .post API 调用,并且需要使用 multipart/form-data。我知道如何使用 JSON 进行调用,但我不熟悉 multipart/form-data。使用 JSON,这是一个 super 简单的调用。只需创建一个类型参数:

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 调用的替代。

最佳答案

文档中的示例:

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)
}
}
)

该方法的完整描述(如果需要设置 header 。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)?)

关于ios - 使用 alamofire 的多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45685210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com