Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a GET request to a webservice from the playframework 2.0

I'm trying to call a webservice from the play framework, and I think I'm doing it wrong. I have an example call to http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml

A snippet from what I'm trying from the playframework is the following:

val response = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get.get()
val body = response.getBody

When I call this, the body consists of "useraccount does not exist". When I just put this url in a browser, I get the response I'm looking for. What am I doing wrong here?

like image 400
Martijn Avatar asked Mar 17 '12 14:03

Martijn


3 Answers

For some reason, I was getting WS from the wrong import. When I fixed the imports to import play.api.libs.ws.WS, it worked. I'm still amazed it worked halfway with the wrong import

like image 194
Martijn Avatar answered Nov 15 '22 05:11

Martijn


Don't know about "useraccount does not exist" but this seems to work:

  val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx?uac=eDKGlpcBQN&query=52.6%2C-4.4&output=xml").get()
  val body = promise.value.get.body

Edit: Removed the space.

Also make sure your editor is not inserting a \n or \r after ?

like image 40
Jamil Avatar answered Nov 15 '22 07:11

Jamil


I know this is old, but I just solved this problem while trying to do the same thing - getting the same results.

GET variables must be passed with WS.url("http://...").setQueryParameter(key, value)

Example:

val promise = WS.url("http://www.myweather2.com/developer/forecast.ashx").setQueryParameter("uac", "eDKGlpcBQN").setQueryParameter("query", "52.6%2C-4.4").setQueryParameter("output", "xml").get()

Annoying, but a relatively simple fix.

like image 42
Scott Stamp Avatar answered Nov 15 '22 05:11

Scott Stamp