Max Tymchii
Max Tymchii

Reputation: 836

AFNetworking slash at the end of URL that added automatically

I have next problem with AFNetworking v.1.x probably it will be the same issue with 2.x

#define LOGIN_URL @"http://myserverr.com/login"

NSURL *url = [NSURL URLWithString:LOGIN_URL relativeToURL:[NSURL URLWithString:LOGIN_URL]];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url ];
    [httpClient postPath:nil
              parameters:@{EMAIL_KEY : email,
                           PASSWORD_KEY : password}
                 success:^(AFHTTPRequestOperation *operation, id responseObject) {

                     id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
                     NSDictionary *result = (NSDictionary *)json;
                     [DCDDownloadHelper loginResult:result];
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                     NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
                     dispatch_async(dispatch_get_main_queue(), ^{

                 }];

But as a result my request is going to url http://myserverr.com/login/ and not to http://myserverr.com/login last slash added automatically in documentation I have found next

 // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
    if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
        url = [url URLByAppendingPathComponent:@""];
    }

But it doesn't help me :)

Upvotes: 2

Views: 621

Answers (1)

Rob
Rob

Reputation: 437592

Given that behavior, you can split your URL into a BASE_URL and a LOGIN_PATH:

#define BASE_URL   @"http://myserverr.com/"
#define LOGIN_PATH @"login"

NSURL *baseURL = [NSURL URLWithString:BASE_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient postPath:LOGIN_PATH
          parameters:@{EMAIL_KEY    : email,
                       PASSWORD_KEY : password}
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 // ...
             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 // ...
             }];

Upvotes: 1

Related Questions