Login IOS with Django server

204 views Asked by At

I have a server written in Django and i want to login from my iOS app. I tried to debug this code for more than a weeks but i can figure out what is the problem. I try to login to my server from a webpage and it works with no error. So i suppose that the server works properly. So i think the problem should be on my iOS request.

I have a ViewController that is a NSURLConnectionDataConnectionDelegate and this is my login function

- (IBAction)Login:(id)sender{
if ([self validateData]) {

    NSURL *URL = [NSURL URLWithString:@"http://127.0.0.1:8000/passapp/login"];

    // Django requires csrf middleware token to work
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
    NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://127.0.0.1:8000/passapp/login"]];
    NSLog(@"cookie csrf %@ /n",    [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
    NSHTTPCookie *csrfCookie;
    NSLog(@"lunghezza array@%lu", (unsigned long)[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]);

    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        if ([cookie.name isEqualToString:@"csrftoken"]) {
            csrfCookie = cookie;
            NSLog(@"CSRF  %@ ",    csrfCookie.value);
        }
    }

    // Setting post request
    NSString *post = [[NSString alloc] initWithFormat:@"username=%@;password=%@;csrfmiddlewaretoken=%@;", [_username text], [_password text], csrfCookie.value];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length] ];

    NSLog(@"post data : %@ /n" , post);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL]]];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:csrfCookie.value forHTTPHeaderField:@"X-CSRFToken"];
    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:postData];
    request.HTTPMethod = @"POST";



    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error) {
                                          NSLog(@"pupu");

                                          if (!error) {
                                              NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
                                              NSLog(@"response: %@",httpResp);
                                              if (httpResp.statusCode == 200) {
                                                  // handle the response here
                                              }
                                          }
                                          else{
                                              NSLog(@"Errore :%@", error);
                                          }

                                          NSLog(@"pupu");
                                      }
                                  ];

    [task resume];

}

}

This is my error on terminal:

[23/Sep/2015 10:28:56]"POST /passapp/login HTTP/1.1" 500 60018

I can't figure out what's wrong with my code and my request. Could someone please help me? Thank you for your time, I hope someone could help me

Cheers

1

There are 1 answers

0
Asig On

Have you seen Sending an HTTP POST request on iOS? I think that it has a good answer which might help you.