2015-09-15 57 views
2

我想有Alamofire发送下面的参数在GET请求,但它发送乱码:URL编码Alamofire GET PARAMS与SwiftyJSON

filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]} 
//www.example.com/example?filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]} 
//Obviously URL encoded 

这是我的代码:

let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] 
let json = JSON(jsonObject) 
print(json) 

输出

{ “$和”:[ { “name”:{ “$ BW”: “公爵” }, “国”: “国标” } ] }

这是我的PARAMS要求:

let params = ["filters" : json.rawValue, "limit":"1", "KEY":"my_key"] 

这是AlamoFire是什么发送:

KEY=my_key& 
filters[$and][][country]=gb& 
filters[$and][][name][$bw]=duke& 
limit=1 

正如你所看到的filter参数是一个完整的混乱。我究竟做错了什么?

+0

你能在这里复制?这个请求是否使用GET发送?也许你可以尝试使用POST方法? – tgebarowski

+0

它必须得到。我使用'.request(.GET,url,params)' – Mika

回答

14

默认情况下,Alamofire使用POST主体中的参数列表对参数进行编码。尝试将编码更改为JSON。这样Alamofire会序列化字典作为一个JSON字符串作为你希望:

let parameters = [ 
    "foo": [1,2,3], 
    "bar": [ 
     "baz": "qux" 
    ] 
] 

Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters, encoding: .JSON) 
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}} 

或使用代码:

let string = "duke" 
let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] 
let json = JSON(jsonObject) 
let params = ["filters" : json.rawValue, "limit":"1", "KEY":"my_key"] 

Alamofire.request(.POST, "http://httpbin.org/post", parameters: params, encoding: .JSON) 
    .responseString(encoding: NSUTF8StringEncoding) { request, response, content, error in 
     NSLog("Request: %@ - %@\n%@", request.HTTPMethod!, request.URL!, request.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "") 
     if let response = response { 
      NSLog("Response: %@\n%@", response, content ?? "") 
     } 
} 

获取输出:

Request: POST - http://httpbin.org/post 
{"filters":{"$and":[{"name":{"$bw":"duke"},"country":"gb"}]},"limit":"1","KEY":"my_key"} 

编辑:GET参数中的URL编码JSON

如果你想在你必须先产生JSON字符串,然后把它作为你的参数字典中的字符串GET参数发送URL编码的JSON:

SWIFT 1

let string = "duke" 
let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] 
let json = JSON(jsonObject) 

// Generate the string representation of the JSON value 
let jsonString = json.rawString(encoding: NSUTF8StringEncoding, options: nil)! 
let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] 


Alamofire.request(.GET, "http://httpbin.org/post", parameters: params) 
    .responseString(encoding: NSUTF8StringEncoding) { request, response, content, error in 
     NSLog("Request: %@ - %@\n%@", request.HTTPMethod!, request.URL!, request.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "") 
     if let response = response { 
      NSLog("Response: %@\n%@", response, content ?? "") 
     } 
} 

SWIFT 2

let string = "duke" 
let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] 
let json = JSON(jsonObject) 

// Generate the string representation of the JSON value 
let jsonString = json.rawString(NSUTF8StringEncoding)! 
let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] 

Alamofire.request(.GET, "http://httpbin.org/post", parameters: params) 
    .responseString(encoding: NSUTF8StringEncoding) { request, response, result in 
     NSLog("Request: %@ - %@\n%@", request!.HTTPMethod!, request!.URL!, request!.HTTPBody.map { body in NSString(data: body, encoding: NSUTF8StringEncoding) ?? "" } ?? "") 
     switch result { 
     case .Success(let value): 
      NSLog("Response with content: %@", value) 
     case .Failure(let data, _): 
      NSLog("Response with error: %@", data ?? NSData()) 
     } 
} 

SWIFT 3和4 Alamofire。0

let string = "duke" 
let jsonObject = ["$and":[["name":["$bw":string], "country":"gb"]]] 
let json = JSON(jsonObject) 

// Generate the string representation of the JSON value 
let jsonString = json.rawString(.utf8)! 
let params = ["filters" : jsonString, "limit": "1", "KEY": "my_key"] 

Alamofire.request("http://httpbin.org/post", method: .get, parameters: params) 
    .responseString { response in 
     #if DEBUG 
      let request = response.request 
      NSLog("Request: \(request!.httpMethod!) - \(request!.url!.absoluteString)\n\(request!.httpBody.map { body in String(data: body, encoding: .utf8) ?? "" } ?? "")") 
      switch response.result { 
      case .success(let value): 
       print("Response with content \(value)") 
      case .failure(let error): 
       print("Response with error: \(error as NSError): \(response.data ?? Data())") 
      } 
     #endif 
} 

这将生成以下URL GET请求:

http://httpbin.org/post?KEY=my_key&filters=%7B%22%24and%22%3A%5B%7B%22name%22%3A%7B%22%24bw%22%3A%22duke%22%7D%2C%22country%22%3A%22gb%22%7D%5D%7D&limit=1 

这URL解码的是:你如何使用Alamofire发送该请求

http://httpbin.org/post?KEY=my_key&filters={"$and":[{"name":{"$bw":"duke"},"country":"gb"}]}&limit=1 
+0

谢谢(我应该在问题中指定对不起),但我需要做一个获取请求。此外,它需要如下所示:“filters”:“{”$和“:[{”name“:{”$ bw“:”duke“},”country“:”gb“}]}”,“limit “:”1“,”KEY“:”my_key“....不知道是谁写的那个API ...我会在问题中更清楚一些。 – Mika

+0

因此,您需要一个JSON字符串作为GET URL中的URL编码参数。这完全改变了这个问题。看看我的编辑,看看是否符合您的需求。 – redent84

+0

谢谢你的完美!我必须编辑的一件事是'let jsonString = json.rawString(NSUTF8StringEncoding,options:.PrettyPrinted)!',因为你不能提供'nil',而第一个标签是无关的。我将编辑它并编辑问题以使其他人更清楚。 – Mika