replacing %3F instead of `?` in url

时间:2018-02-01 18:35:14

标签: string replace request alamofire swift4

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 ?

2 个答案:

答案 0 :(得分:0)

使用 url 参数发布

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

public var task: Task {
...    
return .uploadCompositeMultipart(multipartData, urlParameters: ["key":"value"])
}

答案 1 :(得分:-1)

我找到了这个问题的解决方案。我们应该删除百分比编码。

        // 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