Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing a simple HTTP GET with Dispatch

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.

like image 729
0__ Avatar asked Dec 11 '22 19:12

0__


1 Answers

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.

like image 86
Travis Brown Avatar answered Jan 02 '23 20:01

Travis Brown