Swift Alamofire 4向没有自定义请求适配器的请求添加标头 [英] Swift Alamofire 4 add header to request without custom request adapter

查看:401
本文介绍了Swift Alamofire 4向没有自定义请求适配器的请求添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迁移到新的alamofire,并且在文档中告诉我,我需要做一个自定义请求适配器才能向请求中添加标头,我认为那到处都不舒服,我只想添加

I am trying to migrate to the new alamofire, and in the docs it tells me i need to do a custom request adapter in order to add headers to requests, i think thats not confortable everywhere and i would like to just add the header in the request instead of having to make an adaper for certain requests, anyone have found a way to do it?

下面我分享了我的旧代码(有评论),我新的代码缺少标题参数。

below i shared my old code (commented) and my new code wich is missing a header parameter.

            Alamofire.request(url, method: .post , parameters: parameters, encoding: JSONEncoding.default)
                .responseJSON { response in
                    if response.result.isSuccess {
                        let time = DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
                        DispatchQueue.main.asyncAfter(deadline: time) {
                            self.loadAreasFromServer();
                            self.busy = false;
                        }
                    }
            }

            /*Alamofire.request(.POST, url,headers:headers,parameters:parameters,
                encoding: .json)
                .responseJSON { response in
                    if response.result.isSuccess {
                        let time = DispatchTime(DispatchTime.now()) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
                        DispatchQueue.main.asyncAfter(deadline: time) {
                            self.loadAreasFromServer();
                            self.busy = false;
                        }
                    }
            }*/


推荐答案

方法签名已更改。这应该对您有用:

The method signature changed. This should work for you:

Alamofire.request(url, method: .post , parameters: parameters, encoding: JSONEncoding.default, headers: headers)
    .responseJSON { response in
        if response.result.isSuccess {
            let time = DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds) + Double(1 * Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
            DispatchQueue.main.asyncAfter(deadline: time) {
                self.loadAreasFromServer();
                self.busy = false;
            }
        }
}

这篇关于Swift Alamofire 4向没有自定义请求适配器的请求添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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