Building a Request value 

To construct a Request value, call Gigahorse.url(...) function:

scala> import gigahorse._, support.asynchttpclient.Gigahorse
import gigahorse._
import support.asynchttpclient.Gigahorse

scala> val url = "http://api.duckduckgo.com"
url: String = http://api.duckduckgo.com

scala> val r = Gigahorse.url(url)
r: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(), None, None, None, None, None, None)

Next you can chain the methods defined on Request to construct new values.

HTTP verbs 

There are methods for HTTP verbs: GET, POST, PATCH, PUT, DELETE, HEAD, and OPTIONS.

scala> import java.io.File
import java.io.File

scala> Gigahorse.url(url).get
res0: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(), None, None, None, None, None, None)

scala> Gigahorse.url(url).post("")
res1: gigahorse.Request = Request(http://api.duckduckgo.com, POST, InMemoryBody([B@4d0bd195), Map(), Map(), None, None, None, None, None, None)

scala> Gigahorse.url(url).post(new File("something.txt"))
res2: gigahorse.Request = Request(http://api.duckduckgo.com, POST, FileBody(something.txt), Map(), Map(), None, None, None, None, None, None)

post(...), put(...), and patch(...) methods have a variety that accepts type paramter A that has a context bounds for A: HttpWrite, so potentially this could be extended to accept any custom types.

  /** Uses GET method. */
  def get: Request                                   = this.withMethod(HttpVerbs.GET)
  /** Uses POST method with the given body. */
  def post[A: HttpWrite](body: A): Request           = this.withMethod(HttpVerbs.POST).withBody(body)
  /** Uses POST method with the given body. */
  def post(body: String, charset: Charset): Request  = this.withMethod(HttpVerbs.POST).withBody(EncodedString(body, charset))
  /** Uses POST method with the given file. */
  def post(file: File): Request                      = this.withMethod(HttpVerbs.POST).withBody(FileBody(file))

Request with authentication 

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.

scala> Gigahorse.url(url).get.withAuth("username", "password", AuthScheme.Basic)
res3: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(), None, Some(Realm(username, password, Basic, true, None, None, None, None, None, None, None, None, None, None, None, None, false, false)), None, None, None, None)

There’s also an overload for withAuth(...) method that accepts a Realm value, which you can use to specify more details.

Request with query parameters 

Parameters can be specified as a series of key/value tuples.

scala> Gigahorse.url(url).get.
         addQueryString(
           "q" -> "1 + 1",
           "format" -> "json"
         )
res4: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(q -> List(1 + 1), format -> List(json)), None, None, None, None, None, None)

Request with content type 

Content-Type header should be specified when posting a text.

scala> import java.nio.charset.Charset
import java.nio.charset.Charset

scala> Gigahorse.url(url).post("some text").
         withContentType(MimeTypes.TEXT, Gigahorse.utf8)
res5: gigahorse.Request = Request(http://api.duckduckgo.com, POST, InMemoryBody([B@242abb7b), Map(Content-Type -> List(text/plain;charset=UTF-8)), Map(), None, None, None, None, None, None)

Request with additional headers 

Headers can be specified as a series of key/value tuples.

scala> Gigahorse.url(url).get.
         addHeaders(
           HeaderNames.AUTHORIZATION -> "bearer ****"
         )
res6: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(Authorization -> List(bearer ****)), Map(), None, None, None, None, None, None)

Request with virtual host 

A virtual host can be specified as a string.

scala> Gigahorse.url(url).get.withVirtualHost("192.168.1.1")
res7: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(), None, None, None, None, Some(192.168.1.1), None)

Request with timeout 

If you wish to specify a request timeout, overriding the one specified by the Config, you can use withRequestTimeout to set a value. An infinite timeout can be set by passing Duration.Inf.

scala> import scala.concurrent._, duration._
import scala.concurrent._
import duration._

scala> Gigahorse.url(url).get.withRequestTimeout(5000.millis)
res8: gigahorse.Request = Request(http://api.duckduckgo.com, GET, EmptyBody(), Map(), Map(), None, None, None, Some(5000 milliseconds), None, None)

Submitting form data 

To build a Request value for posting url-form-encoded data, a Map[String, List[String]] needs to be passed into post.

scala> val r = Gigahorse.url("http://www.freeformatter.com/json-validator.html").
         post(Map("inputString" -> List("{}")))
r: gigahorse.Request = Request(http://www.freeformatter.com/json-validator.html, POST, InMemoryBody([B@76076aea), Map(Content-Type -> List(application/x-www-form-urlencoded)), Map(), None, None, None, None, None, None)

Submitting a file 

A Request value can be created for submitting a file using post, put, or patch method.

scala> Gigahorse.url(url).post(new File("something.txt"))