alamofire没有准备好正确的网址

时间:2018-05-07 19:33:07

标签: ios swift alamofire nsurlconnection

我正在使用alamofire来调用webservice。

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

Params我正在传递如下&amp;它们的类型为[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

我知道我缺少什么?

3 个答案:

答案 0 :(得分:0)

%5B%5D是UrlQueryAllowed编码[]。那个Alamofire再次引人注目 - 它看到routes param是一个字典,所以它传递routes[]=foo&routes[]=bar而不是routes=foo&routes=bar而你的后端抱怨错误的参数(routes是强制性的?)。不太清楚为什么Alamofire会这样做,如果HTTP需要一个数组参数在其键的末尾有[] - 从来不必将数组作为查询参数传递。我只是尝试手动编码routes并将routes: <String>传递给某个分隔符。

或者将我的后端更改为routes[]而不是routes。这似乎更容易解决。

答案 1 :(得分:0)

以下是我做的事情

Swift3

删除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())

Swift2

删除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

答案 2 :(得分:-1)

我认为webmethod等于.post并尝试更改enconding:JSONEncoding.default,在这种情况下,请求不会通过url发送数据,而是从body数据发送数据。那是因为无法通过网址发送复杂数据。还要尝试验证您的端点是否接收具有相同属性名称且类型匹配的对象。我已经与Alamofire进行了多次邮寄请求,他们按预期工作。