diff options
| author | Franck Cuny <franck.cuny@gmail.com> | 2013-11-26 10:36:10 -0800 |
|---|---|---|
| committer | Franck Cuny <franck.cuny@gmail.com> | 2013-11-26 10:36:10 -0800 |
| commit | 8ddf2e94df70707b458528a437759b96046d3e01 (patch) | |
| tree | d442818d92d3c9c6f7fcdc92857a1228963849a1 /_posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.md | |
| parent | Don't need to use the IP in the makefile. (diff) | |
| download | lumberjaph-8ddf2e94df70707b458528a437759b96046d3e01.tar.gz | |
Huge update.
Moved all posts from textile to markdown. Updated all the CSS and
styles. Added a new page for the resume.
Diffstat (limited to '')
| -rw-r--r-- | _posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.md (renamed from _posts/2010-03-19-easily-create-rest-interface-with-the-dancer-1.170.textile) | 50 |
1 files changed, 27 insertions, 23 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.md index 73f04aa..e5f04f7 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.md @@ -1,21 +1,21 @@ --- layout: post -category: perl +summary: In which we see that it's easy to create REST interface with Dancer. title: Easily create REST interface with the Dancer 1.170 --- -This week, with "Alexi":http://www.sukria.net/fr/'s help, "I've been working on":http://github.com/sukria/Dancer on adding auto-(de)serialization to Dancer's request. This features will be available in the next "Dancer":http://perldancer.org/ version, the 1.170 (which will be out before April). +This week, with [Alexi](http://www.sukria.net/fr/)'s help, [I've been working on](http://github.com/sukria/Dancer) to add auto-(de)serialization to Dancer's request. This features will be available in the next [Dancer](http://perldancer.org/) version, the 1.170 (which will be out before April). The basic idea was to provides to developer a simple way to access data that have been send in a serialized format, and to properly serialize the response. At the moment, the supported serializers are : - * Dancer::Serialize::JSON - * Dancer::Serialize::YAML - * Dancer::Serialize::XML - * Dancer::Serialize::Mutable +* Dancer::Serialize::JSON +* Dancer::Serialize::YAML +* Dancer::Serialize::XML +* Dancer::Serialize::Mutable -h3. Configuring an application to use the serializer +## Configuring an application to use the serializer To activate serialization in your application: @@ -29,14 +29,14 @@ or in your configuration file: serializer: "JSON" {% endhighlight %} -h3. A simple handler +## 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) : {% highlight bash %} -dancer -a dancerREST -cd dancerREST -vim dancerREST.pm +% dancer -a dancerREST +% cd dancerREST +% vim dancerREST.pm {% endhighlight %} then @@ -67,8 +67,8 @@ true; We can test if everything works as expected: {% highlight bash %} -plackup app.psgi & -curl -H "Content-Type: application/json" -X POST http://localhost:5000/api/user/ -d '{"name":"foo","id":1}' +% 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 %} @@ -98,23 +98,27 @@ get '/api/user/' => sub { If we want to fetch the full list: -bc. curl -H "Content-Type: application/json" http://localhost:5000/api/user/ +{% highlight sh %} +curl -H "Content-Type: application/json" http://localhost:5000/api/user/ # => [{"name":"foo","id":"1"}] +{% endhighlight %} and a specific user: -bc. curl -H "Content-Type: application/json" http://localhost:5000/api/user/1 +{% highlight sh %} +curl -H "Content-Type: application/json" http://localhost:5000/api/user/1 # => {"name":"foo"} +{% endhighlight %} -h3. The mutable serializer +## The mutable serializer -The mutable serializer will try to load an appropriate serializer guessing from the *Content-Type* and *Accept-Type* header. You can also overload this by adding a *content_type=application/json* parameter to your request. +The mutable serializer will try to load an appropriate serializer guessing from the **Content-Type** and **Accept-Type** header. You can also overload this by adding a **content_type=application/json** parameter to your request. While setting your serializer to mutable, your let your user decide which format they prefer between YAML, JSON and XML. -h3. And the bonus +## And the bonus -Dancer provides now a new method to the request object : *is_ajax*. Now you can write something like +Dancer provides now a new method to the request object : `is_ajax`. Now you can write something like {% highlight perl %} get '/user/:id' => sub { @@ -143,15 +147,15 @@ sub _render_user { If we want to simulate an AJAX query: {% highlight bash %} -curl -H "X-Requested-With: XMLHttpRequest" http://localhost:5000/user/1 +% 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: {% highlight bash %} -curl http://localhost:5000/user/1 +% curl http://localhost:5000/user/1 {% endhighlight %} and the template will be rendered. -Hope you like this new features. I've also been working on something similar for "Tatsumaki":http://github.com/miyagawa/tatsumaki. +Hope you like this new features. I've also been working on something similar for [Tatsumaki](http://github.com/miyagawa/tatsumaki). |
