LoginSignup
11
7

More than 5 years have passed since last update.

[Swift]AlamofireでArrayで囲まれたJSONデータをPOST送信

Posted at

以下のような、Arrayで囲まれたJSONデータ

[
    {
        "name":"山田",
        "email":"yamada@example.com"
    },
    {
        "name":"高橋",
        "email":"takahashi@example.com"
    }
]

AlamofireでPOST送信

let users:[[String:AnyObject]] = [
    ["name":"山田","email":"yamada@example.com"],
    ["name":"高橋","email":"takahashi@example.com"]
]

let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(users, options: [])

Alamofire.request(request)
    .responseJSON { response in
        switch response.result {
        case .Failure(_, let error):
            print(error)
        case .Success(let responseObject):
            print(responseObject)
        }
}

参考

Sending json array via Alamofire

11
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7