Last change
on this file since 322 was
322,
checked in by hendrikvanantwerpen, 13 years ago
|
Added Play! framework and application with Jena dependency. Working on
the basic things now (login/register), after that start implementing
our data model.
|
File size:
1.5 KB
|
Line | |
---|
1 | # Making HTTP Requests |
---|
2 | |
---|
3 | Sometimes we would like to call other HTTP services from within a play application. Play supports this task via its ```play.api.libs.WS``` library which provides a way to make asynchronous HTTP calls. |
---|
4 | |
---|
5 | Any calls made by ```play.api.libs.WS``` should return a ```play.api.libs.concurrent.Promise[play.api.libs.ws.Request]``` which we can later handle with play's asynchronous mechanisms. |
---|
6 | |
---|
7 | # A short introduction |
---|
8 | |
---|
9 | ## Hello Get |
---|
10 | ```scala |
---|
11 | import play.api.libs._ |
---|
12 | //let's try to retrive the value from the promise within 5sec |
---|
13 | val myfeed: Promise[ws.Response] = WS.url("http://mysite.com").get() |
---|
14 | |
---|
15 | val content: String = myfeed.await(5000).get.body |
---|
16 | |
---|
17 | val content: xml.Elem = myfeed.await(5000).get.xml |
---|
18 | |
---|
19 | val content: JsValue = myfeed.await(5000).get.json |
---|
20 | |
---|
21 | val code = myfeed.await(5000).get.status |
---|
22 | ``` |
---|
23 | |
---|
24 | ## Hello Post |
---|
25 | ```scala |
---|
26 | |
---|
27 | //send data as text |
---|
28 | val content: String = WS.url("http://localhost:9001/post").post("content").await(5000).get.body |
---|
29 | content must contain ("content") |
---|
30 | content must contain("AnyContentAsText") |
---|
31 | |
---|
32 | //or as x-www-application/form-urlencoded |
---|
33 | val contentForm: String = WS.url("http://localhost:9001/post").post(Map("param1"->Seq("foo"))).await(5000).get.body |
---|
34 | contentForm must contain ("AnyContentAsUrlFormEncoded") |
---|
35 | contentForm must contain ("foo") |
---|
36 | ``` |
---|
37 | |
---|
38 | one also can add custom request and authentication headers and even a signature. For more information please see the api doc [here](https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/libs/WS.scala) |
---|
39 | |
---|
Note: See
TracBrowser
for help on using the repository browser.