Skip to content

BaseURL request problem. #1049

Closed
Closed
@kamwysoc

Description

@kamwysoc

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?

Activity

pedrovereza

pedrovereza commented on Apr 12, 2017

@pedrovereza
Member

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?

kamwysoc

kamwysoc commented on Apr 13, 2017

@kamwysoc
Author

@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.

pedrovereza

pedrovereza commented on Apr 16, 2017

@pedrovereza
Member

Hey @k8mil thanks for the detailed explanation and examples 👍

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

AndrewSB

AndrewSB commented on Apr 16, 2017

@AndrewSB
Member

Very interesting use case. Thanks for implementing this @pedrovereza 😄

kamwysoc

kamwysoc commented on Apr 17, 2017

@kamwysoc
Author

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

BackWorld

BackWorld commented on May 8, 2021

@BackWorld

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)!
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @pedrovereza@AndrewSB@BasThomas@kamwysoc@BackWorld

        Issue actions

          BaseURL request problem. · Issue #1049 · Moya/Moya