iosobjective-cnetwork-programmingnsurlcomponents

NSURLComponent is not giving right URL when IP address exist in URL


My NSURLComponents is translating '/' to '%2F'. Please see log after 10.32.135.10.

NSURLComponents *urlComponent = [[NSURLComponents alloc] init];
[urlComponent setScheme:@"http"];
[urlComponent setHost:@"10.32.135.10/test.API"];
[urlComponent setPath:@"/api/path"];
urlComponent.queryItems = qItems;

In Log when I executed

po [urlComponent URL]
http://10.32.135.10%2Ftest.API/api/path?asetTypeId=1003

Why backward slash is translated into %2F. Please guide.


Solution

  • Because /test.API should be on the path.

    Let's think about it the other way:

    NSString *urlStr = @"http://10.32.135.10/test.API/api/path?asetTypeId=1003";
    NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlStr];
    NSLog(@"Host: %@", [components host]);
    NSLog(@"Path: %@", [components path]);
    NSLog(@"QueryItems: %@", [components queryItems]);
    

    Output:

    $>Host: 10.32.135.10
    $>Path: /test.API/api/path
    $>QueryItems: (
        "<NSURLQueryItem 0x600000254760> {name = asetTypeId, value = 1003}"
    )
    

    Fix:

    [urlComponent setHost:@"10.32.135.10"];
    [urlComponent setPath:@"/test.API/api/path"];