source: Dev/branches/play-2.0.1/documentation/manual/sandbox/Scalahttp.md @ 322

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
3Sometimes 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
5Any 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
11import play.api.libs._
12//let's try to retrive the value from the promise within 5sec
13val myfeed: Promise[ws.Response] = WS.url("http://mysite.com").get()
14
15val content: String = myfeed.await(5000).get.body
16
17val content: xml.Elem = myfeed.await(5000).get.xml
18
19val content: JsValue = myfeed.await(5000).get.json
20
21val code = myfeed.await(5000).get.status
22```
23
24## Hello Post
25```scala
26
27//send data as text
28val content: String = WS.url("http://localhost:9001/post").post("content").await(5000).get.body
29content must contain ("content")
30content must contain("AnyContentAsText")
31
32//or as x-www-application/form-urlencoded
33val contentForm: String = WS.url("http://localhost:9001/post").post(Map("param1"->Seq("foo"))).await(5000).get.body
34contentForm must contain ("AnyContentAsUrlFormEncoded")
35contentForm must contain ("foo")
36```
37
38one 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.