在Scala中执行HTTP请求 [英] Doing HTTP request in Scala
问题描述
我正在尝试向Web服务发出一个简单的POST请求,该服务在Scala中返回一些XML。
I am trying to issue a simple POST request to a webservice which returns some XML in Scala.
似乎 Dispatch 是用于此任务的标准库,但我找不到它的文档。我在上面链接的主站点详细解释了什么是承诺以及如何进行异步编程,但实际上并没有记录API。有一个周期表 - 看起来有点可怕 - 但它似乎只是有用对于已经知道该做什么的人,只需要提醒一下神秘的语法。
It seems that Dispatch is the standard library used for this task, but I cannot find documentation for it. The main site, which I link above, explains at length what is a promise and how to do asynchronous programming, but does not actually document the API. There is a periodic table - which looks a bit scary - but it only seems useful to people who already know what to do and only need a reminder for the cryptic syntax.
似乎 Scalaz有一些HTTP设施,但我不能找到它的任何文档。
It also seems that Scalaz has some facility for HTTP, but I cannot find any documentation for it either.
推荐答案
我使用以下内容: https://github.com/scalaj/scalaj-http 。
这是一个简单的GET请求:
Here's a simple GET request:
import scalaj.http.Http
Http("http://foo.com/search").param("q", "monkeys").asString
以及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可通过SBT获得:
Scalaj HTTP is available through SBT:
libraryDependencies += "org.scalaj" % "scalaj-http_2.11" % "2.3.0"
这篇关于在Scala中执行HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!