Swift: making url from string relative to base url
Asked Answered
F

4

7

Having simple endpoint enum, like:

enum TraccarEndpoint: Endpoint {

    case server

    var baseURL: URL {
        return URL(string: "http://demo.traccar.org/api")!
    }

    var path: String {
        switch self {
        case .server:
            return "/server"
        }
    }

    var url: URL {
        let path = self.path
        let baseURL = self.baseURL
        let url = URL(string: path, relativeTo: baseURL)
        return url!
    }
}

expecting self.url = "http://demo.traccar.org/server", but self.url = "/server -- ttp://www.traccar.org/api". What is that?

Fare answered 9/2, 2017 at 11:7 Comment(1)
you can simplify your url property var url: URL { return URL(string: path, relativeTo: baseURL)! } Also no need to include slash in your server string return "server"Pelagia
C
5

It's the way an URL is described. Your url is correct don't worry. Print url.absoluteString to convince yourself

You can get absolute URL with url.absoluteURL

Crap answered 9/2, 2017 at 11:16 Comment(2)
Not really, little slash made huge difference. When slash ('/server') was in path component I got error (using url parameter as URLConvertible in Alamofire.request call, BUT when I've included that slash to baseURL and removes it from path everything works!Fare
And in both cases (Success and Failure) url.absoluteString was the same...Fare
M
2

I found the solution moving the / from the path to the baseURL:

http://demo.traccar.org/api/ 

instead of

 http://demo.traccar.org/api

and: server instead of /server

Mulligatawny answered 19/2, 2018 at 9:44 Comment(0)
A
1

Below are a few examples of how baseURL and relative paths interact:

NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
[NSURL URLWithString:@"foo" relativeToURL:baseURL];                  // http://example.com/v1/foo
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
[NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
[NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
Aspirin answered 22/5, 2020 at 14:49 Comment(0)
T
0

well, I copy your Code

enum TraccarEndpoint {

    case server

    var baseURL: URL {
        return URL(string: "http://demo.traccar.org/api")!
    }

    var path: String {
        switch self {
        case .server:
            return "/server"
        }
    }

    var url: URL {
        let path = self.path
        let baseURL = self.baseURL
        let url = URL(string: path, relativeTo: baseURL)
        return url!
    }
}

The url string is /server -- http://demo.traccar.org/api.

The different thing is TraccarEndpoint do not inheritance Endpoint. Does EndPoint has baseURL String is "http://www.traccar.org/api" ? If it is , you can see this web Method Dispatch in Swift

It can help you know the Method Dispatch about swift.

Tassel answered 9/2, 2017 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.