ScalaJ-HTTP examples: GET, POST, handling redirects

If you ever need some good ScalaJ-HTTP examples, see the test files in the project, including this HttpBinTest.scala file. That file currently shows a number of good ScalaJ-HTTP examples, including GET, POST, and redirect examples with Scala.

See that page for a full list of examples, but for my own use, here are a few of them.

Handling redirect requests with ScalaJ-HTTP

First, here’s how to NOT follow a redirect request:

@Test
def redirectShouldNotFollow {
    val response = Http("http://httpbin.org/redirect-to?url=http://foo.org").asString
    assertEquals(302, response.code)
    assertEquals(Some("http://foo.org"), response.header("Location"))
}

Second, here’s how you should follow an HTTP redirect request with ScalaJ-HTTP:

@Test
def shouldFollowHttpsRedirect {
    val response = Http("http://httpbin.org/redirect-to?url=https://httpbin.org/get")
        .option(HttpOptions
        .followRedirects(true))
        .asString
    assertEquals(200, response.code)
}

ScalaJ-HTTP GET request example

This example shows how to do an HTTP GET request:

@Test
def gzipDecode {
    val response = Http("http://httpbin.org/gzip").asString
    assertEquals(200, response.code)
    assertEquals("{", response.body.substring(0,1))
}

Setting ScalaJ-HTTP timeout values

I prefer to write HTTP requests that always have explicit timeout values, so here’s one of my own ScalaJ-HTTP examples:

// get the contents of the twitter page (ScalaJ)
val response: HttpResponse[String] = Http("https://twitter.com/jamie_allen")
                                    .timeout(connTimeoutMs = 2000, readTimeoutMs = 5000)
                                    .asString
val html = response.body
println(html)

A ScalaJ-HTTP POST request example

This example shows a ScalaJ-HTTP MultiPart POST:

@Test
def postMulti {
    val response = Http("http://httpbin.org/post")
        .postMulti(
            MultiPart(name="file1", filename="foo.txt", mime="text/text", data="Hello!"),
            MultiPart(name="file2", filename="bar.txt", mime="text/text", data="Goodbye!")
        )
        .param("param1", "a")
        .param("param2", "b")
        .asString
    val binResponse = Json.parse[BinResponse](response.body)
    assertEquals("should have two files", 2, binResponse.files.size)
    assertEquals(Some("Hello!"), binResponse.files.get("file1"))
    assertEquals(Some("Goodbye!"), binResponse.files.get("file2"))
    assertEquals(Some("a"), binResponse.form.get("param1"))
    assertEquals(Some("b"), binResponse.form.get("param2"))
}

This example shows an HTTP POST, such as to a form:

@Test
def postForm {
    val response = Http("http://httpbin.org/post")
        .postForm
        .param("param1", "a")
        .param("param2", "b")
        .asString
    val binResponse = Json.parse[BinResponse](response.body)
    assertEquals(Some("a"), binResponse.form.get("param1"))
    assertEquals(Some("b"), binResponse.form.get("param2"))
}

This Scala code shows another HTTP POST example:

@Test
def postData {
    val response = Http("http://httpbin.org/post")
        .param("param1", "a")
        .param("param2", "b")
        .postData("foo")
        .asString
    val binResponse = Json.parse[BinResponse](response.body)
    assertEquals(Some("a"), binResponse.args.get("param1"))
    assertEquals(Some("b"), binResponse.args.get("param2"))
}

ScalaJ-HTTP: Summary

That file shows some other examples, such as how to handle cookies with ScalaJ-HTTP. See their other files for a few other examples of how to use their library. I hope to add more ScalaJ-HTTP examples over time, and here are links to a couple I have written so far:

In summary, if you want to see some ScalaJ-HTTP examples, including GET and POST requests, and how to handle redirects, I hope this is helpful.