LoginSignup
5
6

More than 5 years have passed since last update.

Alamofire JSONをPOST

Posted at

パラメータにJSONを指定する例

curlだとこう

curl -X POST \
  http://api.exsample.com/api/v1/login \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
    "auth": {
        "phone_number": "090xxxxxxxx",
        "password": "password"
    }
}'

Alamofireの場合


let parameters = [
    "auth": [
             "phone_number": "090xxxxxxxx",
             "password": "password"
            ]
    ]

Alamofire.request("http://api.exsample.com/api/v1/login",
  method: .post, 
  parameters: parameters, 
  encoding: JSONEncoding.default, headers: nil)
.responseJSON { response in
    if let result = response.result.value as? [String: Any] {
       print(result)
    }
}
5
6
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
5
6