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, redirect examples with Scala.

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

Back to top

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)
}
Back to top

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))
}
Back to top

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)
Back to top

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"))
}
Back to top

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.

Back to top

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.