LoginSignup
3
0

More than 5 years have passed since last update.

VaporでHTTPリクエストをなげる

Last updated at Posted at 2017-08-23

Vaporを最近触っています。
HTTPリクエストが簡単だったのでメモ。

環境

  • Vapor 2.0

やり方

サーバー自体を管理するDropletのインスタンスメソッドclient.get(_:)を実行するとOK。
引数は文字列で叩きたいAPIのURL。
client.get(_:).jsonでレスポンスのjsonデータを取得できる。

エンドポイント/qiitaSchemaにGETメソッドでqiitaのスキーマを叩くAPIをこんな感じでかける。

main.swift
let config = try Config()
try config.setup()

let drop = try Droplet(config)
drop.get("qiitaSchema") { req in

    guard let resultJson = try drop.client.get("http://qiita.com/api/v2/schema").json else {
        throw Abort.serverError
    }
    return resultJson
}

 参考

3
0
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
3
0