如何将 Alamofire 与自定义标头一起用于 POST 请求 [英] How to use Alamofire with custom headers for POST request

查看:33
本文介绍了如何将 Alamofire 与自定义标头一起用于 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有自定义标头的 Alamofire 实现了一个 POST 请求,因为我们使用 OAuth2,并且我们必须在标头内的每个请求中发送一个访问令牌.在这种情况下,我必须使用自定义标题.

I implemented a POST request with Alamofire with a custom header, because we work with OAuth2 and we have to send an access token in every request inside the header. In this case I have to use a custom header.

HTTP 标头字段 Authorization 的访问令牌值对我不起作用.服务器生成错误,因为带有访问令牌的 OAuth 标头信息不可用.

The access token value for the HTTP header field Authorization does not work for me. The server generates an error because the header information for OAuth with the access token is not available.

但是我的代码有什么错误?

But what is the mistake in my code?

这是我当前的代码:

let URL =  NSURL(string: url + "/server/rest/action")
var mutableURLRequest = NSMutableURLRequest(URL: URL!)
mutableURLRequest.setValue("Bearer (accessToken)", forHTTPHeaderField: "Authorization")

//this method does not work anymore because it returns an error in the response
//Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Bearer (accessToken)"]

Alamofire.Manager.sharedInstance
    .request(.POST, mutableURLRequest, parameters: parameters, encoding: .JSON)
    .validate()
    .responseJSON {
                (request, response, data, error) -> Void in

                NSLog("REQUEST: (request)")
                NSLog("RESPONSE: (response)")
                NSLog("DATA: (data)")
                NSLog("ERROR: (error)")
    }

推荐答案

下面是我如何使用自定义标题的示例:

Here's an example of how I use it with custom headers:

    var manager = Manager.sharedInstance
    // Specifying the Headers we need
    manager.session.configuration.HTTPAdditionalHeaders = [
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "application/vnd.lichess.v1+json",
        "X-Requested-With": "XMLHttpRequest",
        "User-Agent": "iMchess"
    ]

现在,无论您何时发出请求,它都会使用指定的标头.

Now whenever you make a request, it'll use the specified headers.

您的代码已重构:记得import Alamofire

    let aManager = Manager.sharedInstance
    manager.session.configuration.HTTPAdditionalHeaders = [
        "Authorization": "Bearer (accessToken)" ]

    let URL =  url + "/server/rest/action"

    request(.POST, URL, encoding: .JSON)
        .responseJSON {
            (request, response, data, error) -> Void in

            println("REQUEST: (request)")
            println("RESPONSE: (response)")
            println("DATA: (data)")
            println("ERROR: (error)")
    }

这是请求签名request(method: Method, URLString: URLStringConvertible>, parameters: [String : AnyObject]?, encoding: ParameterEncoding)

如您所见,您不必在其中传递 NSURL,只需传递 URL 的字符串,Alamofire 会负责其余的工作.

As you can see you don't have to pass an NSURL in it, just the string of the URL, Alamofire takes care of the rest.

这篇关于如何将 Alamofire 与自定义标头一起用于 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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