0

i have this problem when i want call this method in request. It seems to be replacing %3f instead of ? in swift4

its my ApiRouter

struct ApiRouter {
enum Router: URLRequestConvertible {

   case getAllPlcae(id: Int, paseSize: Int, pageNumber: Int, countryID: Int, cityID: Int)

    var method: Alamofire.HTTPMethod {
        switch self {

       case .getAllPlcae:
            return .get

        }
    }

    func asURLRequest() throws -> URLRequest {
        let result: (path: String, parameters: [String: AnyObject]?) = {
            switch self {

            case .getAllPlcae(let id, let pageSize, let pageNumber, let countryID, let cityID):
                return("Location?textToSearch=&tagIds=&id=\(id)&pageSize=\(pageSize)&pageNumber=\(pageNumber)&countryID=\(countryID)&cityID=\(cityID)",nil)

            }
        }()


        // MARK: - Set HTTP Header Field
        let url = URL(string: Constants.ApiConstants.baseURLString)!
        var urlRequest = URLRequest(url: url.appendingPathComponent(result.path))
        urlRequest.httpMethod = method.rawValue
        if let token = User.getToken() {
            urlRequest.setValue(token, forHTTPHeaderField: "Authorization")
        }
        let encoding = try Alamofire.URLEncoding.default.encode(urlRequest, with: result.parameters)

        return encoding
    }
}}

when i call this request , just like down

Alamofire.request(ApiRouter.Router.getAllPlcae(id: 0, paseSize: 10, pageNumber: 1, countryID: 0, cityID: 0)).responseArray { (response: DataResponse<[Place]>) in }

its my url request

Location%3FtextToSearch=&tagIds=&id=0&pageSize=10&pageNumber=1&countryID=0&cityID=0

It seems to be replacing %3f instead of ?

how can fix it ?

Amir Daliri
  • 111
  • 2
  • 4
  • You shouldn't be putting your query parameters in as part of the path, you should be returning a dictionary with your parameters in it as the second value in that tuple. – dan Feb 01 '18 at 18:57
  • @dan how ? can you give me a sample code ? – Amir Daliri Feb 01 '18 at 19:10
  • you need to create string of your url and set URLEncoding like https://stackoverflow.com/a/51894621/3110023 – iman Aug 17 '18 at 11:43
  • you need to pass the _query string KVs_ through the `parameters` dictionary, rather than trying to hack it into the path manually. – holex Dec 10 '18 at 15:50

2 Answers2

0

I found the solution for this question. We should remove Percent Encoding.

        // MARK: - Set HTTP Header Field
        let base = URL(string: Constants.ApiConstants.baseURLString)!
        let baseAppend = base.appendingPathComponent(result.path).absoluteString.removingPercentEncoding
        let url = URL(string: baseAppend!)
        var urlRequest = URLRequest(url: url!)
        urlRequest.httpMethod = method.rawValue
        if let token = User.getToken() {
            urlRequest.setValue(token, forHTTPHeaderField: "Authorization")
        }
        let encoding = try Alamofire.URLEncoding.default.encode(urlRequest, with: result.parameters)

        return encoding
Andrew Fan
  • 1,313
  • 5
  • 17
  • 29
Amir Daliri
  • 111
  • 2
  • 4
0

Post with url parameters

https://github.com/Moya/Moya/issues/1030#issuecomment-646910369

public var task: Task {
...    
return .uploadCompositeMultipart(multipartData, urlParameters: ["key":"value"])
}
norbDEV
  • 4,795
  • 2
  • 37
  • 28