2017-08-22 4 views
0

私のiOSアプリケーションでは、Alamofire 4を使用しており、クエリパラメータをバックエンドに要求したいと思っています。しかしAlamofireは "?" URLクエリ(http://blahblahblah/mobile-proxy/authorizations%3FphoneNumber=+555555555&userID=agent)で "%3F"に変更され、バックエンドから404エラーが発生します。 URLEncodingについては読んでいますが、URLRequestでカスタムルータのenumファイルを使用しているため、URLRequestでどのように使用できるのかわかりません。これは私のルーターファイルの一部である:私はAlamofire.URLEncoding.queryString.encodeを試しAlamofireのクエリパラメータ

func asURLRequest() throws -> URLRequest { 

     let url = try Router.baseUrl.asURL() 

     var urlRequest = URLRequest(url: url.appendingPathComponent(path)) 
     urlRequest.httpMethod = method.rawValue 

     switch self { 

     case .authorizations: 
      urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 
      urlRequest.setValue(getHeaderCredentials().operationID, forHTTPHeaderField: Constants.operationID) 
      urlRequest.setValue(getHeaderCredentials().appCode, forHTTPHeaderField: Constants.appCode) 
     case .startAuthentication, .startContractOperation: 
      urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 
      urlRequest.setValue(getHeaderCredentials().appCode, forHTTPHeaderField: Constants.appCode) 
     case .operationAllowance: 
      urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 
     default: 
      break 
     } 

     switch self { 

     case .authorizations: 
      urlRequest = try Alamofire.URLEncoding.queryString.encode(urlRequest, with: nil) 
     default: 
      break 
     } 

     return urlRequest 
    } 

が、それは働いていません。

答えて

4

あなたのURLの文字列を作成する必要があると

let urlwithPercent = yourURlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed()) 
var urlRequest = URLRequest(url: URL(string: urlwithPercent)) 

のようにURLエンコード設定ここyourURlStringは(http://blahblahblah/mobile-proxy/authorizations%3FphoneNumber=+555555555&userID=agent

+0

ありがとう文字列として完全なURLです!それは魅力のように働いています! –

関連する問題