2017-06-29 111 views
2

Postman API添加到下面。 enter image description here如何使用Alamofire 4在Swift 3中使用參數和正文發送POST請求以獲取JSON數據?

本次代碼:

let baseUrl = "abc.com/search/" 
let param = [ 
     "page":"1", 
     "size":"5", 
     "sortBy":"profile_locality" 
    ] 

    let headers = [ 
     "Content-Type": "application/json" 
    ] 


    Alamofire.SessionManager.default.request("\(baseUrl)field", method: .post,parameters: param, encoding: JSONEncoding.default, headers: headers).responseJSON { response in 
     print(response.request ?? "no request") // original URL request 
     if(response.response?.statusCode != nil){ 
      print("done") 
      if self.checkResponse(response.response!.statusCode){ 
       let json = JSON(data: response.data!) 
       //print("at_LeadStop json \(json)") 
       return completionHandler(json, false) 

      } else { 
       return completionHandler(JSON.null, true) 
      } 
     } else { 
      print("gone") 
      return completionHandler(JSON.null, true) 
     }} 

我不知道如何通過這個代碼添加身體的要求。請幫我解決這個問題。

+0

你不通過''中的參數parameters' param' –

+0

https://開頭計算器.com/questions/42696851/get-request-with-parameters來構造URL?和[{fieldName}]參數中的內容 – Larme

+0

感謝@NiravD發表評論。我有傳遞參數的參數。 –

回答

4

你在這裏混合兩件事,pagesizesortBy你需要傳遞url字符串作爲查詢字符串。現在你的身體請求是JSON數組,你可以使用URLRequest來發布數組Alamofire。所以試試這個。

let baseUrl = "abc.com/search/" 
let queryStringParam = [ 
    "page":"1", 
    "size":"5", 
    "sortBy":"profile_locality" 
] 
//Make first url from this queryStringParam using URLComponents 
var urlComponent = URLComponents(string: baseUrl)! 
let queryItems = queryStringParam.map { URLQueryItem(name: $0.key, value: $0.value) } 
urlComponent.queryItems = queryItems 

//Now make `URLRequest` and set body and headers with it 
let param = [ 
    [ 
     "fieldName" : "abc", 
     "fieldValue":"xyz" 
    ], 
    [ 
     "fieldName" : "123", 
     "fieldValue":"789" 
    ] 
] 
let headers = [ "Content-Type": "application/json" ] 
var request = URLRequest(url: urlComponent.url!) 
request.httpMethod = "POST" 
request.httpBody = try? JSONSerialization.data(withJSONObject: param) 
request.allHTTPHeaderFields = headers 

//Now use this URLRequest with Alamofire to make request 
Alamofire.request(request).responseJSON { response in 
    //Your code 
} 
+0

感謝您的幫助@Nirav D –

+0

@KushalShrestha歡迎隊友:) –

+0

謝謝Nirav ...花了半天后發現這:) :) – Ameer

0

試試這個:使用自定義編碼

struct JSONStringArrayEncoding: ParameterEncoding { 
    private let array: [[String : Any]] 

    init(array: [[String : Any]]) { 
     self.array = array 
    } 

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest { 
     var urlRequest = try urlRequest.asURLRequest() 

     let data = try JSONSerialization.data(withJSONObject: array, options: []) 

     if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil { 
      urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") 
     } 

     urlRequest.httpBody = data 

     return urlRequest 
    } 
} 

調用

let values = [ 
      [ 
       "fieldName" : "abc", 
       "fieldValue":"xyz" 
      ], 
      [ 
       "fieldName" : "123", 
       "fieldValue":"789" 
      ] 
     ] 

     let param = [ 
      "page":"1", 
      "size":"5", 
      "sortBy":"profile_locality" 
     ] 

     let parameterEncoding = JSONStringArrayEncoding.init(array: values) 

     Alamofire.request("url", method: .post, parameters: param, encoding:parameterEncoding ).validate().response { (responseObject) in 
      // do your work 
     } 
+0

謝謝爲你的幫助@KKRocks –

相關問題