4

The following is a valid query in a browser (e.g. Firefox):

http://www.freesound.org/api/sounds/search/?q=barking&api_key=074c0b328aea46adb3ee76f6918f8fae

yielding a JSON document:

{
    "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", 
 ...

I am trying to perform this query with Dispatch 0.9.4. Here's a build.sbt:

scalaVersion := "2.10.0"

libraryDependencies += "net.databinder.dispatch" %% "dispatch-core" % "0.9.4"

From sbt console, I do the following:

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)

But the promise always completes with the following error:

res0: dispatch.Promise[String] = Promise(!Unexpected response status: 301!)

So what am I doing wrong? Here is the API documentation in case it helps.

Urist McDev
  • 498
  • 3
  • 14
0__
  • 66,707
  • 21
  • 171
  • 266
  • What does `q OK as.String` print? (Also, you do know that 301 is the "moved permanently" code, right?) – Rex Kerr Dec 30 '12 at 14:46
  • @rex `(http://www.freesound.org/api/sounds/search GET,dispatch.OkFunctionHandler@7fc4fe21)`. I have no good knowledge of HTTP, but it works perfectly if I put the url in the browser. – 0__ Dec 30 '12 at 15:05

1 Answers1

13

You can enable redirect following with the configure method on the Http executor:

Http.configure(_ setFollowRedirects true)(q OK as.String)

You could also pull the Location out of the 301 response manually, but that's going to be a lot less convenient.

Travis Brown
  • 138,631
  • 12
  • 375
  • 680
  • Great, thanks Rex and Travis; I have no clue how this redirection works etc., but this solution works! – 0__ Dec 30 '12 at 15:14
  • 1
    A server X gives me a redirection (status code 302) and also sends me several cookies. This line of code works great : I am redirected and I send the right cookies. Nevertheless, I lose the information of the cookies that were sent with the redirection : is there a way to get them (except by handling manually the redirection?) – JohnCastle Mar 05 '13 at 21:08