我试图让Alamofire在GET请求中发送以下参数,但它发送乱码:
filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]}
//www.example.com/example?filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]}
//Obviously URL encoded
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]]
let json = JSON(jsonObject)
print(json)
Run Code Online (Sandbox Code Playgroud)
输出
{"$ and":[{"name":{"$ bw":"duke"},"country":"gb"}]}
这是我的params请求:
let params = ["filters" : json.rawValue, "limit":"1", "KEY":"my_key"]
Run Code Online (Sandbox Code Playgroud)
这就是AlamoFire发送的内容:
KEY=my_key&
filters[$and][][country]=gb&
filters[$and][][name][$bw]=duke&
limit=1
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,filter参数完全混乱.我究竟做错了什么?
当我尝试从内部 Web 服务器获取 json 时,我需要应对挑战。我从上一个问题中遵循了这一点。这是我的代码
let defaultManager: Alamofire.SessionManager = {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"myhttpsinternaldomain.org": .disableEvaluation
]
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
return Alamofire.SessionManager(
configuration: configuration,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}()
let url = URL(string: urlString)
let username = "user"
let password = "password"
let header = ["user": username, "password": password]
defaultManager.request(url!, method: .get, headers: header).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
print("JSON: \(json)")
case .failure(let error): …Run Code Online (Sandbox Code Playgroud)