I'm trying POST NSMutableURLRequest request through Alamofire, but not able to set these filed value in HTTP header. Any help ?
var params = ["username":"[email protected]", "password":"Pasword123!", "csrfmiddlewaretoken":csrfToken] as Dictionary<String, String>
var post:NSString = "csrfmiddlewaretoken=Basic \(csrfToken)&[email protected]&password=Password123!"
var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
var URL = NSURL(string: "https://myurl/account/login")
var mutableURLRequest = NSMutableURLRequest(URL: URL!)
mutableURLRequest.setValue("Content-Type", forHTTPHeaderField: "application/x-www-form-urlencoded")
mutableURLRequest.setValue("keep-alive", forHTTPHeaderField: "Connection")
mutableURLRequest.setValue("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36", forHTTPHeaderField: "User-Agent")
mutableURLRequest.setValue("Code", forHTTPHeaderField: "X-CSRFToken")
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.HTTPBody = postData
let username = "[email protected]"
let password = "Password123!"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
mutableURLRequest.setValue(base64LoginString, forHTTPHeaderField: "Authorization")
let manager = Alamofire.Manager.sharedInstance // or create a new one
let request = manager.request(mutableURLRequest)
Alamofire.request(.POST, "https://url/account/login",parameters: params)
.responseString{ (request, response, data, error) in
println("Request :\(request)\n\n")
println("Response: \(response)\n\n")
println("Data\(data)")
println("Error : \(error)")
println("Done")
}
Why do you have the line
Alamofire.request(.POST, "https://url/account/login",parameters: params)
, when you've gone through all the trouble to actually make anNSURLRequest
object?Just remove it: