Contents
はじめに
これもまたお約束なので、以下のような流れで対応します。
ソースコード
コードは以下の通り。
let urlString = "http://hogehoge.com/api/get_profile"
// 指定先URL
var request = URLRequest(url: URL(string:urlString)!)
// URLRequestを作ります
// set the method(HTTP-POST)
request.httpMethod = "POST"
// set the header(s)
let params: [String: Any] = [
"my_account_id": "141",
"get_account_id": "143" ]
// 引数は画面から届く
var httpBodyTxt = ""
for (key, value) in params{
if(!httpBodyTxt.isEmpty){
httpBodyTxt = httpBodyTxt + "&"
}
httpBodyTxt = httpBodyTxt + key
httpBodyTxt = httpBodyTxt + "="
httpBodyTxt = httpBodyTxt + (value as! String)
}
request.httpBody = httpBodyTxt.data(using: .utf8)
// use NSURLSessionDataTask
let task = URLSession.shared.dataTask(with: request, completionHandler: {
data, response, error in
// 何がしかの処理
})
task.resume()なお飽くまで私の環境で動いたソースコードですので、コピペでは動かない可能性があります。