alamofire标头+参数

时间:2016-02-18 04:33:11

标签: ios swift http-headers alamofire

Hello World,

我来找你,因为我尝试在同一个函数中发送带参数的标题:

Alamofire.Manager.sharedInstance.request(.PUT, "url", headers: headers, parameters: parameters)

但也许你已经知道只发送了标题。

我也这样试过:

 let manager = Alamofire.Manager.sharedInstance
 manager.session.configuration.HTTPAdditionalHeaders = [
            "Authorization": token]

 manager.request(.PUT, "http://192.168.99.100:3030/users/\(identity)", parameters: parameters, encoding:.JSON)

但不会发送标题..

在alamofire中实现标头的简单方法是什么?

先谢谢; - )

的问候,

2 个答案:

答案 0 :(得分:8)

像其他参数一样在字典中设置标题并将其传递到标题中。例如

let Auth_header    = [ "Authorization" : token ]

Alamofire.Manager.sharedInstance.request(.PUT, "url", headers: Auth_header, parameters: parameters)  

您可以查看详细信息HTTP Basic Authentication

HTTP基本身份验证

请求上的身份验证方法将在适当时自动为NSURLAuthenticationChallenge提供NSURLCredential:

let user = "user"
let password = "password"

Alamofire.request(.GET, "https://httpbin.org/basic-auth/\(user)/\(password)")
     .authenticate(user: user, password: password)
     .responseJSON { response in
         debugPrint(response)
     }

根据您的服务器实现,Authorization标头也可能是合适的:

 let user = "user"
 let password = "password"

 let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
 let base64Credentials = credentialData.base64EncodedStringWithOptions([])

 let headers = ["Authorization": "Basic \(base64Credentials)"]

 Alamofire.request(.GET, "https://httpbin.org/basic-auth/user/password", headers: headers)
     .responseJSON { response in
         debugPrint(response)
     }

答案 1 :(得分:0)

我已经按照您在第一条评论中所说的做了,但无论如何只发送了标题:

$dom = new DOMDocument();

$dom->load('new_result.xml');

$results = $dom->documentElement;

foreach( $results  as $result)

{

foreach( $result->getElementsByTagName('youtube') as $youtube )

{

echo ' video url ' . $youtube->nodeValue;

}

}

已解决:问题在于我没有在参数

中提供编码
let parameters : [String : NSString] = [
            "username": username,
            "email": email,
            "currentPassword": currentpassword,
            "newPassword": newpassword]        
let Auth_header    = [ "Authorization" : token ]

Alamofire.Manager.sharedInstance.request(.PUT, "url", headers: Auth_header, parameters: parameters)

感谢EI Captain