gpt4 book ai didi

ios - alamofire 没有准备正确的 url

转载 作者:行者123 更新时间:2023-11-28 09:23:24 25 4
gpt4 key购买 nike

我正在使用 alamofire 调用网络服务。

request(url, method: webMethod, parameters: webParams, encoding: URLEncoding.default, headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { (response) in
print("responseresponse===\(response.request)")

我的成绩低于日志。

responseresponse===http://www.mywebsite.com/api/searchFlight?
adults=3&charter=true&directFlight=true&
flexibleDays=0&flightClass=PREMIUM_ECONOMY&
offerLimit=10&refundableOnly=false&
routes%5B%5D=KWI-BOM%2C2018-08-09&routes%5B%5D=BOM-KWI%2C2018-08-25

我传递的参数如下,它们是 [String: Any] 类型

[
"adults": 3,
"directFlight": "true",
"flightClass": "PREMIUM_ECONOMY",
"refundableOnly": "false",
"offerLimit": 10,
"flexibleDays": "0",
"charter": "true",
"routes": ["KWI-BOM,2018-08-09", "BOM-KWI,2018-08-25"]
]

这会返回状态代码 400,这意味着请求无效。

我不明白为什么路由会给出错误的输出?路线如下

routes%5B%5D=KWI-BOM%2C2018-08-09&routes%5B%5D=BOM-KWI%2C2018-08-25
^^^^^^ ^^^^^^

我原以为路线如下。

routes=KWI-BOM%2C2018-08-09&routes=BOM-KWI%2C2018-08-25

我知道我错过了什么吗?

最佳答案

下面是我做的

swift 3

移除 GET 请求的方括号

struct CustomGetEncoding: ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try URLEncoding().encode(urlRequest, with: parameters)
request.url = URL(string: request.url!.absoluteString.replacingOccurrences(of: "%5B%5D=", with: "="))
return request
}
}

...

Alamofire.request("http://example.com", method: .get, parameters: ["foo": ["bar1", "bar2"]], encoding: CustomGetEncoding())

移除 POST 请求的方括号

struct CustomPostEncoding: ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try URLEncoding().encode(urlRequest, with: parameters)
let httpBody = NSString(data: request.httpBody!, encoding: String.Encoding.utf8.rawValue)!
request.httpBody = httpBody.replacingOccurrences(of: "%5B%5D=", with: "=").data(using: .utf8)
return request
}
}

...

Alamofire.request("http://example.com", method: .post, parameters: ["foo": ["bar1", "bar2"]], encoding: CustomPostEncoding())

swift 2

移除 GET 请求的方括号

let parameterEncoding = ParameterEncoding.Custom { requestConvertible, parameters in
let (mutableRequest, error) = ParameterEncoding.URL.encode(requestConvertible, parameters: parameters)
mutableRequest.URL = NSURL(string: mutableRequest.URLString.stringByReplacingOccurrencesOfString("%5B%5D=", withString: "="))
return (mutableRequest, error)
}

Alamofire.request(.GET, "http://example.com", parameters: ["foo": ["bar1", "bar2"]], encoding: parameterEncoding)

移除 POST 请求的方括号

let parameterEncoding = ParameterEncoding.Custom { requestConvertible, parameters in
let (mutableRequest, error) = ParameterEncoding.URL.encode(requestConvertible, parameters: parameters)
let httpBody = NSString(data: mutableRequest.HTTPBody!, encoding: NSUTF8StringEncoding)!
mutableRequest.HTTPBody = httpBody.stringByReplacingOccurrencesOfString("%5B%5D=", withString: "=").dataUsingEncoding(NSUTF8StringEncoding)
return (mutableRequest, error)
}

Alamofire.request(.POST, "http://example.com", parameters: ["foo": ["bar1", "bar2"]], encoding: parameterEncoding)

Reference

关于ios - alamofire 没有准备正确的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com