/docs/02-request.md

https://github.com/eed3si9n/gigahorse · Markdown · 122 lines · 88 code · 34 blank · 0 comment · 0 complexity · 18077c7c268c581f4c8dc06121a2c681 MD5 · raw file

  1. ---
  2. out: request.html
  3. ---
  4. Building a Request value
  5. ------------------------
  6. To construct a `Request` value, call `Gigahorse.url(...)` function:
  7. ```console:new
  8. scala> import gigahorse._, support.asynchttpclient.Gigahorse
  9. scala> val url = "https://api.duckduckgo.com"
  10. scala> val r = Gigahorse.url(url)
  11. ```
  12. Next you can chain the methods defined on `Request` to construct new values.
  13. ### HTTP verbs
  14. There are methods for HTTP verbs: GET, POST, PATCH, PUT, DELETE, HEAD, and 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(...)`, and `patch(...)` methods have a variety that accepts
  22. type paramter `A` that has a context bounds for `A: HttpWrite`,
  23. so potentially this could be extended to accept any custom types.
  24. ```scala
  25. /** Uses GET method. */
  26. def get: Request = this.withMethod(HttpVerbs.GET)
  27. /** Uses POST method with the given body. */
  28. def post[A: HttpWrite](body: A): Request = this.withMethod(HttpVerbs.POST).withBody(body)
  29. /** Uses POST method with the given body. */
  30. def post(body: String, charset: Charset): Request = this.withMethod(HttpVerbs.POST).withBody(EncodedString(body, charset))
  31. /** Uses POST method with the given file. */
  32. def post(file: File): Request = this.withMethod(HttpVerbs.POST).withBody(FileBody(file))
  33. ```
  34. ### Request with authentication
  35. If you need to use HTTP authentication, you can specify it in the request, using a username, password, and an AuthScheme. Valid case objects for the AuthScheme are `Basic`, `Digest`, `NTLM`, `Kerberos`, and `SPNEGO`.
  36. ```console
  37. scala> Gigahorse.url(url).get.withAuth("username", "password", AuthScheme.Basic)
  38. ```
  39. There's also an overload for `withAuth(...)` method that accepts a `Realm` value,
  40. which you can use to specify more details.
  41. ### Request with query parameters
  42. Parameters can be specified as a series of key/value tuples.
  43. ```console
  44. scala> Gigahorse.url(url).get.
  45. addQueryString(
  46. "q" -> "1 + 1",
  47. "format" -> "json"
  48. )
  49. ```
  50. ### Request with content type
  51. `Content-Type` header should be specified when posting a text.
  52. ```console
  53. scala> import java.nio.charset.Charset
  54. scala> Gigahorse.url(url).post("some text").
  55. withContentType(MimeTypes.TEXT, Gigahorse.utf8)
  56. ```
  57. ### Request with additional headers
  58. Headers can be specified as a series of key/value tuples.
  59. ```console
  60. scala> Gigahorse.url(url).get.
  61. addHeaders(
  62. HeaderNames.AUTHORIZATION -> "bearer ****"
  63. )
  64. ```
  65. ### Request with virtual host
  66. A virtual host can be specified as a string.
  67. ```console
  68. scala> Gigahorse.url(url).get.withVirtualHost("192.168.1.1")
  69. ```
  70. ### Request with timeout
  71. If you wish to specify a request timeout, overriding the one specified by the `Config`,
  72. you can use `withRequestTimeout` to set a value.
  73. An infinite timeout can be set by passing `Duration.Inf`.
  74. ```console
  75. scala> import scala.concurrent._, duration._
  76. scala> Gigahorse.url(url).get.withRequestTimeout(5000.millis)
  77. ```
  78. ### Submitting form data
  79. To build a `Request` value for posting url-form-encoded data, a `Map[String, List[String]]` needs to be passed into `post`.
  80. ```console
  81. scala> val r = Gigahorse.url("http://www.freeformatter.com/json-validator.html").
  82. post(Map("inputString" -> List("{}")))
  83. ```
  84. ### Submitting a file
  85. A `Request` value can be created for submitting a file using `post`, `put`, or `patch` method.
  86. ```
  87. scala> Gigahorse.url(url).post(new File("something.txt"))
  88. ```