Moya: BaseURL request problem.

Created on 12 Apr 2017  路  6Comments  路  Source: Moya/Moya

Moya : 8.0.3
Language : Swift 3.1

Let's imagine that you have a link that looks like that:

let urlString = "https://google.com/123/somepath?X-ABC-Asd=123"

When I do my request using Alamofire :

Alamofire.upload(fileURL, to: URL(string: urlString)!, method: .put, headers: nil).response {
    response in
    print(response)
}

it all seems fine(don't look at status code):

screen shot 2017-04-12 at 14 14 40

but when I do the same request using Moya like that:

enum MyApi {
    case upload
}
extension MyApi : TargetType {
    var baseURL: URL {
        let urlString = "https://google.com/123/somepath?X-ABC-Asd=123"
        return URL(string: urlString)!        
    }

    var path: String {
        return ""
    }

    ...
}

provider.request(MyApi.upload)

The created request URL is different that in Alamofire request.

screen shot 2017-04-12 at 14 20 49

The difference is that before ? character moya inserts the / char.

https://google.com/123/somepath?X-ABC-Asd=123
https://google.com/123/somepath/?X-ABC-Asd=123

Could you help me and tell me what I'm doing wrong?

bug? question

All 6 comments

Hey @k8mil, can you check if it works as expected when you change your baseURL to be just https://google.com and path to return /123/somepath?X-ABC-Asd=123?

@pedrovereza
Thanks for your response.

I've created a sample repo with the test code. Just clone and run it.

https://github.com/k8mil/MoyaBaseURL

Below you can see, screen from Charles with 3 options:

First

request was performed with Alamofire using
URL(string: "https://google.com/123/somepath?X-ABC-Asd=123")!

Second

request was performed using MoyaProvider with
baseURL = "https://google.com/123/somepath?X-ABC-Asd=123"

Third

request was performed using MoyaProvider with
baseURL = "https://google.com and path = "/123/somepath?X-ABC-Asd=123"

screen shot 2017-04-13 at 22 19 42

And only Alamofire produces that what I want to achieve :)

Important thing is that I'm using Moya in my project and the consuming my backend API with Moya is really great and works perfectly, but I have some case that I have to use the URL(which is not related with backedAPI) that I received from some service and I shouldn't modify that URL. Unfortunately, as you can see in the Second and Thrid example the URL is modified by removing/changing the ? char.

Hey @k8mil thanks for the detailed explanation and examples 馃憤

I think I have a fix in #1053, care to take a look? 馃槈

Very interesting use case. Thanks for implementing this @pedrovereza 馃槃

Thanks @pedrovereza !
I've tested it and it works fine.
So, now I wait till it will be merged with master 馃憤 :)

Modify the source code in file URL+Moya.swift as below:

public extension URL {

    /// Initialize URL from Moya's `TargetType`.
    init<T: TargetType>(target: T) {
        // When a TargetType's path is empty, URL.appendingPathComponent may introduce trailing /, which may not be wanted in some cases
        // See: https://github.com/Moya/Moya/pull/1053
        // And: https://github.com/Moya/Moya/issues/1049
        let targetPath = target.path
        if targetPath.isEmpty {
            self = target.baseURL
        } else {
            let tag = target.baseURL.absoluteString.hasSuffix("/") ? "" : "/"
            self = .init(string: target.baseURL.absoluteString + tag + targetPath)!
        }
    }
}

Was this page helpful?
0 / 5 - 0 ratings