【发布时间】:2016-09-15 12:29:59
【问题描述】:
那里。我遇到了一个非常奇怪的问题。问题是,当我尝试发送 PATCH 请求时,服务器说没有授权标头包含令牌。 PUT 请求也是如此。尝试嗅探并发现根本没有发送授权标头。而任何其他类型的请求都包含授权标头。首先认为它的 Alamofire 框架特定问题,但是使用 NSURLConnection 请求和 NSURLSession 任务给了我同样的结果:没有发送授权头!
这是我用于 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")
}
【问题讨论】:
标签: ios swift nsurlconnection alamofire