/docs/ja/02-request.md

https://github.com/eed3si9n/gigahorse · Markdown · 121 lines · 87 code · 34 blank · 0 comment · 0 complexity · 1db808ba217922c5ef79fb41af096bf1 MD5 · raw file

  1. ---
  2. out: request.html
  3. ---
  4. Request 値の構築
  5. ---------------
  6. `Request` 値の構築を構築するには `Gigahorse.url(...)` 関数を呼び出す:
  7. ```console:new
  8. scala> import gigahorse._, support.okhttp.Gigahorse
  9. scala> val url = "https://api.duckduckgo.com"
  10. scala> val r = Gigahorse.url(url)
  11. ```
  12. 次に`Request` のメソッドをつなげていくことで新しい `Request` 値を作っていく
  13. ### HTTP verb
  14. HTTP verb (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS) それぞれに対してメソッドがある
  15. ```console
  16. scala> import java.io.File
  17. scala> Gigahorse.url(url).get
  18. scala> Gigahorse.url(url).post("")
  19. scala> Gigahorse.url(url).post(new File("something.txt"))
  20. ```
  21. `post(...)``put(...)``patch(...)` メソッドは `A: HttpWrite` という context bound のある
  22. 型パラメータ `A` を受け取るオーバーロードがあるためあらゆるカスタム型に対応できるように拡張することができる
  23. ```scala
  24. /** Uses GET method. */
  25. def get: Request = this.withMethod(HttpVerbs.GET)
  26. /** Uses POST method with the given body. */
  27. def post[A: HttpWrite](body: A): Request = this.withMethod(HttpVerbs.POST).withBody(body)
  28. /** Uses POST method with the given body. */
  29. def post(body: String, charset: Charset): Request = this.withMethod(HttpVerbs.POST).withBody(EncodedString(body, charset))
  30. /** Uses POST method with the given file. */
  31. def post(file: File): Request = this.withMethod(HttpVerbs.POST).withBody(FileBody(file))
  32. ```
  33. ### Request に認証を付ける
  34. HTTP 認証を使う必要がある場合はユーザ名パスワード`AuthScheme` を用いて `Request` で指定することができる
  35. 有効な `AuthScheme` の値は `Basic` `Digest` `NTLM` `Kerberos` `SPNEGO` となっている
  36. ```console
  37. scala> Gigahorse.url(url).get.withAuth("username", "password", AuthScheme.Basic)
  38. ```
  39. `withAuth(...)` には `Realm` 値を受け取るオーバーロードもあってそれはより細かい設定をすることができる
  40. ### Request にクエリパラメータを付ける
  41. パラーメータはキーと値のタプルで設定できる
  42. ```console
  43. scala> Gigahorse.url(url).get.
  44. addQueryString(
  45. "q" -> "1 + 1",
  46. "format" -> "json"
  47. )
  48. ```
  49. ### Request にコンテンツタイプを付ける
  50. テキストを POST する場合`Content-Type` ヘッダを指定するべきだ
  51. ```console
  52. scala> import java.nio.charset.Charset
  53. scala> Gigahorse.url(url).post("some text").
  54. withContentType(MimeTypes.TEXT, Gigahorse.utf8)
  55. ```
  56. ### Request にその他のヘッダを付ける
  57. ヘッダはキーと値のタプルで設定できる
  58. ```console
  59. scala> Gigahorse.url(url).get.
  60. addHeaders(
  61. HeaderNames.AUTHORIZATION -> "bearer ****"
  62. )
  63. ```
  64. ### Request にバーチャルホストを付ける
  65. バーチャルホストは文字列で設定できる
  66. ```console
  67. scala> Gigahorse.url(url).get.withVirtualHost("192.168.1.1")
  68. ```
  69. ### Request にタイムアウトを付ける
  70. `Config` で指定された値を上書きしてリクエストタイムアウトを指定したい場合は
  71. `withRequestTimeout` を使って設定できる無期限を指定する場合は `Duration.Inf` を渡す
  72. ```console
  73. scala> import scala.concurrent._, duration._
  74. scala> Gigahorse.url(url).get.withRequestTimeout(5000.millis)
  75. ```
  76. ### フォームデータ
  77. フォームエンコードされたデータを POST で送信する `Request` を作るには
  78. `post` `Map[String, List[String]]` を渡す
  79. ```console
  80. scala> val r = Gigahorse.url("http://www.freeformatter.com/json-validator.html").
  81. post(Map("inputString" -> List("{}")))
  82. ```
  83. ### ファイル
  84. `post``put``patch` メソッドを使ってファイル送信のための `Request` 値を作ることができる
  85. ```
  86. scala> Gigahorse.url(url).post(new File("something.txt"))
  87. ```