summaryrefslogtreecommitdiff
path: root/_posts
diff options
context:
space:
mode:
Diffstat (limited to '_posts')
-rw-r--r--_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile27
1 files changed, 19 insertions, 8 deletions
diff --git a/_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile b/_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile
index 20dfe1d..73f04aa 100644
--- a/_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile
+++ b/_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile
@@ -19,20 +19,25 @@ h3. Configuring an application to use the serializer
To activate serialization in your application:
-bc. set serializer => 'JSON';
+{% highlight perl %}
+set serializer => 'JSON';
+{% endhighlight %}
or in your configuration file:
-bc. serializer: "JSON"
+{% highlight yaml %}
+serializer: "JSON"
+{% endhighlight %}
h3. A simple handler
-Let's create a new dancer application (you can fetch the source on
-[github](http://github.com/franckcuny/dancerREST) :
+Let's create a new dancer application (you can fetch the source on "github":http://github.com/franckcuny/dancerREST :
-bq. dancer -a dancerREST
+{% highlight bash %}
+dancer -a dancerREST
cd dancerREST
vim dancerREST.pm
+{% endhighlight %}
then
@@ -61,9 +66,11 @@ true;
We can test if everything works as expected:
-bq. plackup app.psgi &
+{% highlight bash %}
+plackup app.psgi &
curl -H "Content-Type: application/json" -X POST http://localhost:5000/api/user/ -d '{"name":"foo","id":1}'
# => {"name":"foo","id":"1"}
+{% endhighlight %}
Now we add a method to fetch a list of users, and a method to get a
specific user:
@@ -135,11 +142,15 @@ sub _render_user {
If we want to simulate an AJAX query:
-bc. curl -H "X-Requested-With: XMLHttpRequest" http://localhost:5000/user/1
+{% highlight bash %}
+curl -H "X-Requested-With: XMLHttpRequest" http://localhost:5000/user/1
+{% endhighlight %}
and we will obtain our result in JSON. But we can also test without the X-Requested-With:
-bc. curl http://localhost:5000/user/1
+{% highlight bash %}
+curl http://localhost:5000/user/1
+{% endhighlight %}
and the template will be rendered.