24

I'm trying to send an authentication string via cookie in a NSMutableURLRequest. I'm trying to create the NSHTTPCookie through

 +(id)cookieWithProperties:(NSDictionary *)properties

But nowhere have I been able to find how to specify the properties other than the simple key-value pair I have for authentication. When I only use my key-value pair, nil is returned.

Any examples, documentation, or thoughts on this would be greatly appreciated.

Michael Grinich
  • 4,770
  • 8
  • 29
  • 30
  • Please post the code you're using to create the cookie; did you take a look at http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/doc/uid/20001702-425346 – sbooth Mar 28 '09 at 03:53

5 Answers5

40

I noticed on, on my 2.2.1 iPhone, that the cookie didn't get created if NSHTTPCookiePath is not specified, even though it is shown as "optional" in the docs:

So, I do:

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"example.com", NSHTTPCookieDomain,
                            @"/", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
jm.
  • 23,422
  • 22
  • 79
  • 93
21

This is how you set properties in a cookie:

 NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                              url, NSHTTPCookieOriginURL,
                              @"testCookies", NSHTTPCookieName,
                              @"1", NSHTTPCookieValue,
                              nil];
  NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

In the example above: url, testCookies, and 1 are the values. Likewise, NSHTTPCookieOriginURL, NSHTTPCookieName, NSHTTPCookieValue are the keys for the NSDictionary object, as in key-value pairs.

You set/get properties using NSDictionary and add to NSHTTPCookie.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Jordan
  • 21,746
  • 10
  • 51
  • 63
  • 4
    Actually i was not able to get it work, only with NSHTTPCookieDomain, and NSHTTPCookiePath. See jm's answer. Also: http://lists.apple.com/archives/Webkitsdk-dev/2003/Sep/msg00003.html – mfazekas Mar 28 '10 at 08:42
  • I was able to use NSHTTPCookieOriginURL as long as I *also* specified NSHTTPCookiePath (thanks to jm below for the hint). Note that you can use the path method on the url to provide the value (i.e. [url path]). – Kris Giesing Jun 03 '12 at 20:56
7

I could not get that to work.

I got this to work however:

NSMutableURLRequest* ret = [NSMutableURLRequest requestWithURL:myURL];
[ret setValue:@"myCookie=foobar" forHTTPHeaderField:@"Cookie"];
titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
7

I've found one mistake in jm's example: NSHTTPCookiePath should be @"/", but not @"\\\\".

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"example.com", NSHTTPCookieDomain,
                            @"/", NSHTTPCookiePath,  // IMPORTANT!
                            @"testCookies", NSHTTPCookieName,
                            @"1", NSHTTPCookieValue,
                            nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];

NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

[request setAllHTTPHeaderFields:headers];
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Serhiy
  • 332
  • 6
  • 13
  • I updated my answer. Seems reasonable, although I don't have an iphone to test on anymore. – jm. Oct 22 '13 at 04:59
0

key NSHTTPCookiePath should exist in dictionary when using

[NSHTTPCookie cookieWithProperties:dictionary]

method whether using NSHTTPCookieDomain or NSHTTPCookieOriginURL. And value for NSHTTPCookiePath should be @"/" not @"\\".

Baby Groot
  • 4,637
  • 39
  • 52
  • 71