以下是浏览器(例如 Firefox)中的有效查询:
http://www.freesound.org/api/sounds/search/?q=barking&api_key=074c0b328aea46adb3ee76f6918f8fae
生成 JSON 文档:
{
"num_results": 610,
"sounds": [
{
"analysis_stats": "http://www.freesound.org/api/sounds/115536/analysis/",
"analysis_frames": "http://www.freesound.org/data/analysis/115/115536_1956076_frames.json",
"preview-hq-mp3": "http://www.freesound.org/data/previews/115/115536_1956076-hq.mp3",
"original_filename": "Two Barks.wav",
"tags": [
"animal",
"bark",
"barking",
"dog",
"effects",
...
我正在尝试使用 Dispatch 0.9.4 执行此查询.这是一个 build.sbt
:
scalaVersion := "2.10.0"
libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.9.4"
在 sbt console
中,我执行以下操作:
import dispatch._
val q = url("http://www.freesound.org/api/sounds/search")
.addQueryParameter("q", "barking")
.addQueryParameter("api_key", "074c0b328aea46adb3ee76f6918f8fae")
val res = Http(q OK as.String)
但 promise 总是以以下错误完成:
res0: dispatch.Promise[String] = Promise(!Unexpected response status: 301!)
那我做错了什么? Here is the API documentation以防万一。
最佳答案
您可以在 Http
执行器上使用 configure
方法启用重定向:
Http.configure(_ setFollowRedirects true)(q OK as.String)
您也可以手动将 Location
从 301
响应中提取出来,但这会很不方便。
关于scala - 使用 Dispatch 执行简单的 HTTP GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14091583/