Doing HTTP request in Scala

前端 未结 9 755
死守一世寂寞
死守一世寂寞 2020-12-12 14:31

I am trying to issue a simple POST request to a webservice which returns some XML in Scala.

It seems that Dispatch is the standard library used for this task, but I

9条回答
  •  庸人自扰
    2020-12-12 15:00

    I use the following: https://github.com/scalaj/scalaj-http.

    Here's a simple GET request:

    import scalaj.http.{Http, HttpOptions}
    
    Http("http://example.com/search").param("q", "monkeys").asString
    

    and an example of a POST:

    val result = Http("http://example.com/url").postData("""{"id":"12","json":"data"}""")
      .header("Content-Type", "application/json")
      .header("Charset", "UTF-8")
      .option(HttpOptions.readTimeout(10000)).asString
    

    Scalaj HTTP is available through SBT:

    libraryDependencies += "org.scalaj" % "scalaj-http_2.11" % "2.3.0"
    

提交回复
热议问题