Alamofire cookie request

I am starting and I cannot figure out how to make a .GET request (however it requires authorization) with Alamofire. I managed to do this with another web service (login), because it takes an argument parameter:

parameters = [ "username" : username "password" : password ] 

Then:

 Alamofire.request(.POST, loginUrl, parameters: parameters).responseJSON { (request, response, data, error) -> Void in //handling the response } 

In the response header, I get some information:

 [Transfer-Encoding: Identity, Server: nginx/1.4.1, Content-Type: application/json, P3P: policyref="http://www.somewebpage.com", CP="NON DSP COR CURa TIA", Connection: keep-alive, Date: Sun, 08 Mar 2015 13:49:20 GMT, Vary: Accept-Encoding, Cookie, Set-Cookie: sessionid=5xeff47e65f674a4cc5b2d54f344304b; Domain=.somedomain.com; Path=/, tbauth=1; Domain=.somedomain.com; Path=/, Content-Encoding: gzip] 

This is the type [NSObject : AnyObject]

What should I do with this information in order to store it in NSURLDefaults and prepare a valid request parameter (cookie)? Do I need all the fields or just a Set-Cookie?

I tried to manually set the parameter:

 parameters = [ "Cookie" : "sessionid=5xeff47e65f674a4cc5b2d54f344304b; Domain=.somedomain.com; Path=/, tbauth=1; Domain=.somedomain.com; Path=/" ] 

but it returns error NSURLErrorDomain -1017 (NSURLErrorCannotParseResponse)

Thanks for all the answers.

+6
source share
1 answer

Ok, after 2 weeks I found a solution:

 let URL = NSURL(string: query)! let mutableUrlRequest = NSMutableURLRequest(URL: URL) mutableUrlRequest.HTTPMethod = "GET" let prefs = NSUserDefaults.standardUserDefaults() let cookie = prefs.valueForKey("COOKIE") as String mutableUrlRequest.setValue(cookie, forHTTPHeaderField: "Cookie") Alamofire.request(mutableUrlRequest).responseJSON { (request, response, data, error) -> Void in //handling the response } 
+14
source

Source: https://habr.com/ru/post/983502/


All Articles