- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在进行 .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/
我需要做一个 POST请求带有 JSON 的 HTTP 正文对象,但我还需要在同一个请求中使用 url 查询参数。 POST: http://www.example.com/api/create?pa
这里是原始的 github issue , 支持者建议在这里开一个帖子以获得更多公众支持。 我正在使用 xcode 6.4。 $ pod --version 0.38.2 我的播客文件: platfo
我在项目中使用cocoapods安装alamofire,它在模拟器中运行良好。当我想在 iphone 6s(ios 13.3.1) 上运行我的应用程序时,它崩溃并显示错误消息。我的 Xcode 版本是
如何取消 Alamofire 共享管理器中的所有请求? 这是我的功能: class func cancelAllRequests() { Alamofire.Manager.sharedIns
我想有多个适配器到同一个 SessionManager,这可能吗? 我的用例是: 从适配器设置默认 header 如果是 basic_auth:添加 basic_auth_adapter 如果是tok
调用 Alamofireobject 映射器的正确方法吗? 有人对我的问题提出建议吗? 最佳答案 func postRequestSample() { let
我是 IOS 的新手,这是我的第一个项目,我想在我的项目中使用 Alamofire 库 我按照所有步骤使用 Cocoapods 安装库,一切都应该正常工作,但我收到这个错误“No这样的模块'Alamo
我是 Swift 的新手,来自 Alamorefire Referencee , 你可以做以下的请求 Alamofire.request(.GET, "http://httpbin.org/get")
默认情况下,Alamofire 发送一个包含 gzip 的 Accept-Encoding header 。我如何告诉它停止这样做?我确实接受 gzip,我很高兴 Alamofire 为我解析它,但是
AFImage 的新功能。我不确定这是否是获取图像并将其缓存的正确方法。似乎每次运行时它都没有访问服务器,但我不确定它是否被缓存了?我走运了?看来我在下面使用的语法也过时了...... 任何评论表示赞
我检查了新的 Alamofire 安装步骤。 由于我需要针对 iOS 7.0,我想知道是否导入 Alamofire.swift是否足以让它工作? 为什么文档声明将函数包裹在 Struct Alamof
在 Alamofire 5 Beta 中,SessionManager已被 Session 取代. 我想知道现在分配 RequestAdapter 的过程是什么,因为这是一个 var在 Alamofi
任何人都可以提出一些关于如何使用 header 扩展 alamofire 的建议,例如需要在发送之前设置的 Content-MD5? 最佳答案 这是一个有点老的问题,但我遇到了同样的问题,我使用以下代
有没有人看到用指纹而不是公钥来固定 Alamofire 的方法? 对不起,如果这已经得到回答,我还没有在任何地方看到它。 谢谢 最佳答案 这最终变得非常简单。下面的代码可能并不完美,我的真实代码正在做
我想运行一个 Alamofire 请求,该请求使用先前 Alamofire 请求的结果作为参数。为了简单起见: //Code1 Alamofire.request("URL", met
当我尝试在应用程序中的 Xcode 中运行我的 iOS 应用程序时: dyld: Library not loaded: @rpath/Alamofire.framework/Alamofire
我想运行一个使用先前 Alamofire 请求的结果作为参数的 Alamofire 请求。为简单起见: //Code1 Alamofire.request("URL", method:
更新:我解决了这个问题。请在下面查看我的回答(在问题和评论下方)。 这个问题被标记为重复,但它是不同的,因为它是一个全新的错误,我无法通过任何搜索找到它。 我尝试将 Alamofire 安装到我的 X
我想先 POST 到一个网站,然后 GET 另一个网站获取数据。 并更改 POST 中的 key ,并在 for 循环中继续这样做 5 次以获得不同的数据。 但是,程序总是先运行 POST 5 次,然
我正在使用 Alamofire 5(测试版 1)为 WooCommerce 编写 API 客户端,这将允许我获取订单、优惠券等以及创建它们。注意我使用的是新的 .responseDecodable功能
我是一名优秀的程序员,十分优秀!