ios - 为什么授权 header 在 iOS PATCH 请求中被删除?

标签 ios swift nsurlconnection alamofire

那里。我遇到了一个非常奇怪的问题。问题是,当我尝试发送 PATCH 请求时,服务器说没有授权 header 包含 token 。 PUT 请求也是如此。尝试嗅探并发现根本没有发送授权 header 。而任何其他类型的请求都包含授权 header 。首先想到它的 Alamofire 框架特定问题,但是使用 NSURLConnection 请求和 NSURLSession 任务给了我同样的结果:没有发送授权 header !

这是我用于 Alamofire 的代码:

Alamofire.request(.PATCH, path, parameters: ["email":"new@mail.com"],     encoding: .JSON, headers: ["Authorization":"token \    ((User.sharedUser().token)!)"]).validate().responseJSON { (response) in
            if response.response?.statusCode == 200{
                print("success")
            }else{
                print("Error")
            }
        }

这是带有 NSURLConnection 的代码:

let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
request.HTTPMethod = "PATCH"
request.addValue("\(token)", forHTTPHeaderField: "authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

do{
    let bodyData = try NSJSONSerialization.dataWithJSONObject(["email":"nuv@gmail.com"], options: [])
    request.HTTPBody = bodyData
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
    {
        (response, data, error) in
        if let mdata = data {
            let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
            print(contents)
        } else {
            print(error?.localizedDescription)
        }
        }
}catch{
    print("failed serialization")
}

最佳答案

IIRC,Authorization header 是 NSURLSession 为自己的目的保留的那些 header 之一,并且可能会用它自己的值覆盖——特别是如果你要发送一些东西这看起来像普通的 HTTP 身份验证。

您可以发送 X-Authorization header 吗?

关于ios - 为什么授权 header 在 iOS PATCH 请求中被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39511078/

相关文章:

ios - swift&parse.com : converting String to AnyObject?

objective-c - sendSynchronousRequest 使用安装在 Mac OS X keychain for Mac 上的自签名证书

cocoa-touch - 计算iOS中给定时间点之前的时间

objective-c - ASIHTTPRequest 和 NSURLRequest 不同的结果

ios - UItableviewcell "cell-identifier"内存管理

ios - 在应用程序中使用 Google map 中的转弯导航

ios - swift - UIAlertController 将用户带回到第一个 ViewController

ios - UIViewController 抛出 'unrecognized selector' 错误

ios - 在 UIProgressView 上添加 NSURLConnection 加载过程

ios - NSURLConnection 异步到底是什么意思?