File: README.mkdn

package info (click to toggle)
libdancer-plugin-rest-perl 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 216 kB
  • sloc: perl: 228; xml: 123; makefile: 2
file content (139 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# NAME

Dancer::Plugin::REST - A plugin for writing RESTful apps with Dancer

# VERSION

version 0.12

# DESCRIPTION

This plugin helps you write a RESTful webservice with Dancer.

# SYNOPSYS

```perl
package MyWebService;

use Dancer;
use Dancer::Plugin::REST;

prepare_serializer_for_format;

get '/user/:id.:format' => sub {
    User->find(params->{id});
};

# curl http://mywebservice/user/42.json
{ "id": 42, "name": "John Foo", email: "john.foo@example.com"}

# curl http://mywebservice/user/42.yml
--
id: 42
name: "John Foo"
email: "john.foo@example.com"
```

# KEYWORDS

## prepare\_serializer\_for\_format

When this pragma is used, a before filter is set by the plugin to automatically
change the serializer when a format is detected in the URI.

That means that each route you define with a **:format** token will trigger a
serializer definition, if the format is known.

This lets you define all the REST actions you like as regular Dancer route
handlers, without explicitly handling the outgoing data format.

## resource

This keyword lets you declare a resource your application will handle.

```perl
resource user =>
    get    => sub { # return user where id = params->{id}   },
    create => sub { # create a new user with params->{user} },
    delete => sub { # delete user where id = params->{id}   },
    update => sub { # update user with params->{user}       };

# this defines the following routes:
# GET /user/:id
# GET /user/:id.:format
# POST /user
# POST /user.:format
# DELETE /user/:id
# DELETE /user/:id.:format
# PUT /user/:id
# PUT /user/:id.:format
```

## helpers

Some helpers are available. This helper will set an appropriate HTTP status for you.

### status\_ok

```perl
status_ok({users => {...}});
```

Set the HTTP status to 200

### status\_created

```perl
status_created({users => {...}});
```

Set the HTTP status to 201

### status\_accepted

```perl
status_accepted({users => {...}});
```

Set the HTTP status to 202

### status\_bad\_request

```perl
status_bad_request("user foo can't be found");
```

Set the HTTP status to 400. This function as for argument a scalar that will be used under the key **error**.

### status\_not\_found

```perl
status_not_found("users doesn't exists");
```

Set the HTTP status to 404. This function as for argument a scalar that will be used under the key **error**.

# LICENCE

This module is released under the same terms as Perl itself.

# AUTHORS

This module has been written by Alexis Sukrieh `<sukria@sukria.net>` and Franck
Cuny.

# SEE ALSO

[Dancer](https://metacpan.org/pod/Dancer) [http://en.wikipedia.org/wiki/Representational\_State\_Transfer](http://en.wikipedia.org/wiki/Representational_State_Transfer)

# AUTHORS

- Alexis Sukrieh <sukria@sukria.net>
- Franck Cuny <franckc@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.