如何在Alamofire中设置requestSerializer [英] How to Set requestSerializer in Alamofire

查看:1677
本文介绍了如何在Alamofire中设置requestSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 swift 中开展一个项目。我使用 Alamofire 获取 REST API
但为了使其正常工作,我需要在<$中传递一个参数c $ c> 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 中相当于 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

或者如果你是ant传递自定义标题,传递字典为

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆