Jac*_*iel 0 rest request ios swift alamofire
在我的iOS应用程序中,我使用Alamofire 4并想要使用查询参数向后端发送请求.但是Alamofire转换了"?" 在url查询中的"%3F"(http:// blahblahblah/mobile-proxy/authorizations%3FphoneNumber = + 555555555&userID = agent),我从后端收到404错误.我读到了有关URLEncoding的内容,但是我找不到任何方法可以将它与URLRequest一起使用,因为我使用带有URLRequest的自定义路由器枚举文件.这是我的路由器文件的一部分:
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
}
Run Code Online (Sandbox Code Playgroud)
我尝试Alamofire.URLEncoding.queryString.encode,但它不起作用.
你需要创建你的网址字符串并设置URLEncoding之类的
let urlwithPercent = yourURlString.addingPercentEncoding( withAllowedCharacters: NSCharacterSet.urlQueryAllowed())
var urlRequest = URLRequest(url: URL(string: urlwithPercent))
Run Code Online (Sandbox Code Playgroud)
这里yourURlString是一个完整的URL作为字符串(http:// blahblahblah/mobile-proxy/authorizations%3FphoneNumber = + 555555555&userID = agent)
| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |