-1

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

boh
  • 27
  • 4
  • Can you post full traceback from your django application? You can simply get it by disabling debug and defining at least one valid email for `ADMINS` in setting, each error will be mailed to that email, unless you've customized loggers. – GwynBleidD Sep 23 '15 at 10:52
  • Thank you for your answer . By disabling debug the error changed from 500 to "GET /passapp/login/ HTTP/1.1" 401 3389" so i can't post full traceback . I don't understand how the error could have changed only by disabling option. By the way, my credentials are correct but i still get error, and the GET request should be a POST... – boh Sep 23 '15 at 12:23
  • Your domain name must be specified inside `ALLOWED_HOSTS` in settings when debug is disabled. – GwynBleidD Sep 23 '15 at 12:25
  • I've done it, ALLOWED_HOSTS = ['localhost', '127.0.0.1'], but no 500 error. – boh Sep 23 '15 at 13:03
  • So what you're getting now? Nothing? Is it working now? – GwynBleidD Sep 23 '15 at 13:04
  • Now i getting this : " "GET /passapp/login/ HTTP/1.1" 401 ". Probably there is something wrong in the iOS POST request, because server receive a GET request with no user and password. – boh Sep 23 '15 at 13:07

1 Answers1

0

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

Community
  • 1
  • 1
Asig
  • 11
  • 3