2017-11-02 9 views
0

次のJSONデータを送信しようとしています。私はAlamofire 4.0を使用しています。 フォームデータ形式でサーバーにデータを渡す方法はありますか?Alamofire 4.0を使用してフォームデータを送信する方法速報でのリクエスト

{ 
    "apikey" : "455feh54b", 
    "action": "ADD", 
    "address1" : "Mumbai", 
    "country" : "India" 
"userInfo" :  { 
     "user_detail" :[ 
       { 
       "name" : "abc", 
       "age" : 15, 
       "location" : "Delhi" 
       }, 
      {"name" : "pqr", 
       "age" : 20, 
       "location" : "Mumbai" 
      } 
      ] 
} 

} 
+0

を送ることができる方法であるあなたは、単純なキーと値のペアを送信することができますか? "apikey"のように: "455feh54b"、 "アクション": "ADD"、 "address1": "ムンバイ"、 –

+0

私は同じ問題があります!!!! –

答えて

0

hereと記載されているとおりです。
次のようにJSONデータを渡すことができます。ここで

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

// Both calls are equivalent 
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default) 
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding(options: [])) 

// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}} 
+0

v4.0のドキュメントが必要な場合は、[こちら](http://cocoadocs.org/docsets/Alamofire/4.0.0/#parameter-encoding) –

0

は、あなたは、単にそれを

let user_detail = ["name":name, "age":age, "location":location] 

let userInfo = ["user_detail": user_detail] 

let params = ["apikey":title, 
         "action":type, 
         "address":time, 
         "userInfo":String.JSONStringify(value: user_detail as AnyObject)] 

      Alamofire.request(url, method:.post, parameters: params, encoding: URLEncoding.default).validate().responseAuthJSON { 
      response in 
      switch response.result { 
      case .failure(let error): 
       print(error) 
       self.showAlert(title: "Saving note failed! Please, try again.", message: "") 
      case .success(let responseObject): 
       print("response is success: \(responseObject)") 

      } 
     } 
関連する問題