Closed
Description
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):
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.
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 commentedon Apr 12, 2017
Hey @k8mil, can you check if it works as expected when you change your
baseURL
to be just https://google.com andpath
to return/123/somepath?X-ABC-Asd=123
?kamwysoc commentedon Apr 13, 2017
@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
andpath = "/123/somepath?X-ABC-Asd=123"
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 theURL
(which is not related with backedAPI) that I received from some service and I shouldn't modify thatURL
. Unfortunately, as you can see in the Second and Thrid example the URL is modified by removing/changing the?
char.pedrovereza commentedon Apr 16, 2017
Hey @k8mil thanks for the detailed explanation and examples 👍
I think I have a fix in #1053, care to take a look? 😉
AndrewSB commentedon Apr 16, 2017
Very interesting use case. Thanks for implementing this @pedrovereza 😄
kamwysoc commentedon Apr 17, 2017
Thanks @pedrovereza !
I've tested it and it works fine.
So, now I wait till it will be merged with
master
👍 :)BackWorld commentedon May 8, 2021
Modify the source code in file
URL+Moya.swift
as below: