Sjakelien
Sjakelien

Reputation: 2317

Passing a Session ID to a GET Objective-C

So, I have to methods to approach a web service:

A GET:

- (NSDictionary *)getDataFromURL:(NSString*)url {

    NSString * serverAddress =[NSString stringWithFormat:url,mySchoolURL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                        URLWithString:serverAddress]
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                       timeoutInterval:10
                                    ];

    [request setHTTPMethod: @"GET"];

    NSError *requestError = nil;
    NSURLResponse *urlResponse = nil;


    NSData *response =  [NSURLConnection
                          sendSynchronousRequest:request
                          returningResponse:&urlResponse
                          error:&requestError];


    NSError* error = nil;
    NSDictionary *output = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:nil];

    return output;

}

and a POST:

-(NSData*)postData:(NSDictionary*)requestData toUrl:(NSString*)destination {
    NSError *error;
    NSData *postdata = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postdata length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:destination,mySchoolURL]]];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:postdata];

    [request setHTTPMethod:@"POST"];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    return nil;
}

Somehow, the HTTP header of the POST method does include the cookie's SessionID, while the GET one doesn't.

I have to admit that I don't fully understand the innards of cookies and the like, but several sources on the web claim that I should't worry about the cookies at all, since they are taken care of automatically.

Anyway, the web service I'm talking to expects a Session ID in both POST and GET situations, so now I'm forced to start understanding what's going on.

Could any of you help me out here? The concrete question is: how to I pass a Session_ID to a URL using a GET method? Thanks ahead

Upvotes: 0

Views: 1479

Answers (1)

Joseph Johns
Joseph Johns

Reputation: 178

Okay so one way to do this is by simply adding the cookies to the HTTP headers. When doing this the correctness of the URL is key. If the protocol is wrong, some cookies may not be included. You will need to get all available cookies, and then create a dictionary for the headers with he available cookies. Then you simply add those headers to your request by calling setAllHTTPHeaderFields:. I placed a quick example of how to do this below, however you can learn more about how cookies work at the Apple Documentation Class Reference

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];
    NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

    [request setAllHTTPHeaderFields:headers];

I really hope this helps you out. I would recommend reading the Apples Documentation to help you out as much as possible. I wish you the best of luck!

Upvotes: 1

Related Questions