如何在 Alamofire 中设置 requestSerializer [英] How to Set requestSerializer in Alamofire
问题描述
我目前正在 swift
中开发一个项目.我将 Alamofire
用于 REST API
但要使其工作,我需要在 requestSerializer
I am currently working on a project in swift
. I used Alamofire
for REST API
but to make it work i need to pass a parameter in requestSerializer
在AFNETWORKING
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
但是我找不到 Alamofire
的任何内容.我是 swift 的新手,请帮助我.
But i can't find anything for Alamofire
.
I'm new to swift please help me out.
推荐答案
您可以将 JSON
数据作为编码参数传递,Alamofire
中的 Encoding
> 等价于 AFJSONRequestSerializer
You can pass pass JSON
data as encoding parameters, Encoding
in Alamofire
is equivalent to AFJSONRequestSerializer
request = Alamofire.request(.POST, webServicesURL, parameters: parameters, encoding: .JSON, headers: self.headers)
如果你想在响应时处理 JSON 数据,只需请求
If you want to handle JSON data on response, just request
//This will give you response in JSON
request?.responseJSON { response in
switch response.result
{
case .Success:
success(response: response.result.value)
case .Failure(let error):
failure(error: error)
}
}
requestJSON
相当于 Alamofire 中的 AFJSONResponseSerializer
requestJSON
is equivalent to AFJSONResponseSerializer
in Alamofire
OR 如果您想传递自定义标题,请将字典传递为
OR If you want to pass custom headers, pass a dictionary as
let headers = [
"Content-Type": "application/json"
]
//Here we are passing the header in header parameter.
request = Alamofire.request(.POST, webServicesURL, parameters: parameters, encoding: .JSON, headers: self.headers)
这篇关于如何在 Alamofire 中设置 requestSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!