diff --git a/feeds/all-en.atom.xml b/feeds/all-en.atom.xml
index 650ac53..711e5bc 100644
--- a/feeds/all-en.atom.xml
+++ b/feeds/all-en.atom.xml
@@ -14,12 +14,13 @@ it is easily pluggable with validations frameworks, such as Collander.</p>
<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
- <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span> <span class="s">'You lack awesomeness!'</span><span class="p">)</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
-<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="p">(</span><span class="n">is_awesome</span><span class="p">))</span>
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
- <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"succeeded"</span><span class="p">}</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"yay!"</span><span class="p">}</span>
</pre></div>
<p>All the errors collected during the validation process, or after, are collected
before returning the request. If any, a error 400 is fired up, with the list of
@@ -30,19 +31,91 @@ formats in the future)</p>
<p><strong>location</strong> is where the error arised. It can either be "body", "query", "headers"
or "path". <strong>name</strong> is the name of the variable causing problem, if any, and
<strong>description</strong> contains a more detailled message.</p>
-<p>Here is an example of a malformed request:</p>
+<p>Let's run this simple service, with <cite>bin/paster serve</cite> and send some queries to
+it:</p>
+<div class="highlight"><pre>$ curl -v http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: */*
+>
+* HTTP 1.0, assume close after body
+< HTTP/1.0 400 Bad Request
+< Content-Type: application/json; charset=UTF-8
+[{"location": "body", "name": "awesome", "description": "You lack awesomeness!"}
+</pre></div>
+<p>I've removed the extra clutter from the curl's output, but you got the general idea.</p>
+<p>The content returned is in JSON, and I know exactly what I have to do: add an
+"awesome" parameter in my query. Let's do it again:</p>
<pre class="literal-block">
-$ # run a demo app
+$ curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "yay!"}
</pre>
-<p>To describe a web service in <em>cornice</em>, you have to write something like this</p>
-<div class="system-message">
-<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 54)</p>
-<p>Content block expected for the "code-block" directive; none found.</p>
+<p>Validators can also attach extra information about validations to the request,
+using <cite>request.validated</cite>. It is a standard dict automatically attached to the
+requests.</p>
+<p>For instance, in our validator, we can chose to validate the parameter passed
+and use it in the body of the webservice:</p>
+<div class="highlight"><pre><span class="n">service</span> <span class="o">=</span> <span class="n">Service</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"service"</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"/service"</span><span class="p">)</span>
+
+
+<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'awesome '</span> <span class="o">+</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span>
+
+
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
+<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]}</span>
+</pre></div>
<pre class="literal-block">
-.. code-block:: python
+curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "awesome yeah"}
+</pre>
+</div>
+<div class="section" id="dealing-with-accept-headers">
+<h2>Dealing with "Accept" headers</h2>
+<p>The HTTP spec defines a <strong>Accept</strong> header the client can send so the response
+is encoded the right way. A resource, available at an URL, can be available in
+different formats. This is especially true for web services.</p>
+<p>Cornice can help you to deal with this. The services you define can tell which
+content-types they can deal with, and this will be checked against the
+<strong>Accept</strong> headers sent by the client.</p>
+<p>Let's refine a bit our previous example, by specifying which content-types are
+supported, using the <cite>accept</cite> parameter:</p>
+<div class="system-message">
+<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 117)</p>
+<p>Error in "code-block" directive:
+1 argument(s) required, 0 supplied.</p>
+<pre class="literal-block">
+.. code-block::
+
+ @service.get(validator=is_awesome, accept=("application/json", "text/json"))
+ def get1(request):
+ return {"test": "yay!"}
</pre>
</div>
+<p>Now, if you specifically ask for XML, for instance, cornice will throw a 406
+with the list of accepted content-types:</p>
+<pre class="literal-block">
+$ curl -vH "Accept: application/xml" http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: application/xml
+>
+< HTTP/1.0 406 Not Acceptable
+< Content-Type: application/json; charset=UTF-8
+< Content-Length: 33
+<
+["application/json", "text/json"]
+</pre>
+</div>
+<div class="section" id="building-your-documentation-automatically">
+<h2>Building your documentation automatically</h2>
+<p>XXX</p>
</div>
How are you handling your shared expenses?2011-10-15T00:00:00+02:00Alexis Métaireautag:blog.notmyidea.org,2011-10-15:/how-are-you-handling-your-shared-expenses.html/<p><strong>TL;DR:</strong> We're kick-starting a new application to manage your shared
expenses. Have a look at <a class="reference external" href="http://ihatemoney.notmyidea.org">http://ihatemoney.notmyidea.org</a></p>
diff --git a/feeds/all.atom.xml b/feeds/all.atom.xml
index 2e4fdc1..4206194 100644
--- a/feeds/all.atom.xml
+++ b/feeds/all.atom.xml
@@ -14,12 +14,13 @@ it is easily pluggable with validations frameworks, such as Collander.</p>
<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
- <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span> <span class="s">'You lack awesomeness!'</span><span class="p">)</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
-<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="p">(</span><span class="n">is_awesome</span><span class="p">))</span>
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
- <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"succeeded"</span><span class="p">}</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"yay!"</span><span class="p">}</span>
</pre></div>
<p>All the errors collected during the validation process, or after, are collected
before returning the request. If any, a error 400 is fired up, with the list of
@@ -30,19 +31,91 @@ formats in the future)</p>
<p><strong>location</strong> is where the error arised. It can either be "body", "query", "headers"
or "path". <strong>name</strong> is the name of the variable causing problem, if any, and
<strong>description</strong> contains a more detailled message.</p>
-<p>Here is an example of a malformed request:</p>
+<p>Let's run this simple service, with <cite>bin/paster serve</cite> and send some queries to
+it:</p>
+<div class="highlight"><pre>$ curl -v http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: */*
+>
+* HTTP 1.0, assume close after body
+< HTTP/1.0 400 Bad Request
+< Content-Type: application/json; charset=UTF-8
+[{"location": "body", "name": "awesome", "description": "You lack awesomeness!"}
+</pre></div>
+<p>I've removed the extra clutter from the curl's output, but you got the general idea.</p>
+<p>The content returned is in JSON, and I know exactly what I have to do: add an
+"awesome" parameter in my query. Let's do it again:</p>
<pre class="literal-block">
-$ # run a demo app
+$ curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "yay!"}
</pre>
-<p>To describe a web service in <em>cornice</em>, you have to write something like this</p>
-<div class="system-message">
-<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 54)</p>
-<p>Content block expected for the "code-block" directive; none found.</p>
+<p>Validators can also attach extra information about validations to the request,
+using <cite>request.validated</cite>. It is a standard dict automatically attached to the
+requests.</p>
+<p>For instance, in our validator, we can chose to validate the parameter passed
+and use it in the body of the webservice:</p>
+<div class="highlight"><pre><span class="n">service</span> <span class="o">=</span> <span class="n">Service</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"service"</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"/service"</span><span class="p">)</span>
+
+
+<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'awesome '</span> <span class="o">+</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span>
+
+
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
+<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]}</span>
+</pre></div>
<pre class="literal-block">
-.. code-block:: python
+curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "awesome yeah"}
+</pre>
+</div>
+<div class="section" id="dealing-with-accept-headers">
+<h2>Dealing with "Accept" headers</h2>
+<p>The HTTP spec defines a <strong>Accept</strong> header the client can send so the response
+is encoded the right way. A resource, available at an URL, can be available in
+different formats. This is especially true for web services.</p>
+<p>Cornice can help you to deal with this. The services you define can tell which
+content-types they can deal with, and this will be checked against the
+<strong>Accept</strong> headers sent by the client.</p>
+<p>Let's refine a bit our previous example, by specifying which content-types are
+supported, using the <cite>accept</cite> parameter:</p>
+<div class="system-message">
+<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 117)</p>
+<p>Error in "code-block" directive:
+1 argument(s) required, 0 supplied.</p>
+<pre class="literal-block">
+.. code-block::
+
+ @service.get(validator=is_awesome, accept=("application/json", "text/json"))
+ def get1(request):
+ return {"test": "yay!"}
</pre>
</div>
+<p>Now, if you specifically ask for XML, for instance, cornice will throw a 406
+with the list of accepted content-types:</p>
+<pre class="literal-block">
+$ curl -vH "Accept: application/xml" http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: application/xml
+>
+< HTTP/1.0 406 Not Acceptable
+< Content-Type: application/json; charset=UTF-8
+< Content-Length: 33
+<
+["application/json", "text/json"]
+</pre>
+</div>
+<div class="section" id="building-your-documentation-automatically">
+<h2>Building your documentation automatically</h2>
+<p>XXX</p>
</div>
Quels usages pour l'informatique ?2011-12-01T00:00:00+01:00Alexis Métaireautag:blog.notmyidea.org,2011-12-01:/quels-usages-pour-linformatique-fr.html/<p>Quand on termine ses études, on s'en pose un tas, des questions. Sur le métier
que l'on veut faire, sur ce que ça signifie, sur le sens et la valeur du
diff --git a/feeds/mozilla.atom.xml b/feeds/mozilla.atom.xml
index 9669fc0..ce3e119 100644
--- a/feeds/mozilla.atom.xml
+++ b/feeds/mozilla.atom.xml
@@ -14,12 +14,13 @@ it is easily pluggable with validations frameworks, such as Collander.</p>
<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
- <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span> <span class="s">'You lack awesomeness!'</span><span class="p">)</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
-<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="p">(</span><span class="n">is_awesome</span><span class="p">))</span>
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
- <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"succeeded"</span><span class="p">}</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="s">"yay!"</span><span class="p">}</span>
</pre></div>
<p>All the errors collected during the validation process, or after, are collected
before returning the request. If any, a error 400 is fired up, with the list of
@@ -30,18 +31,90 @@ formats in the future)</p>
<p><strong>location</strong> is where the error arised. It can either be "body", "query", "headers"
or "path". <strong>name</strong> is the name of the variable causing problem, if any, and
<strong>description</strong> contains a more detailled message.</p>
-<p>Here is an example of a malformed request:</p>
+<p>Let's run this simple service, with <cite>bin/paster serve</cite> and send some queries to
+it:</p>
+<div class="highlight"><pre>$ curl -v http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: */*
+>
+* HTTP 1.0, assume close after body
+< HTTP/1.0 400 Bad Request
+< Content-Type: application/json; charset=UTF-8
+[{"location": "body", "name": "awesome", "description": "You lack awesomeness!"}
+</pre></div>
+<p>I've removed the extra clutter from the curl's output, but you got the general idea.</p>
+<p>The content returned is in JSON, and I know exactly what I have to do: add an
+"awesome" parameter in my query. Let's do it again:</p>
<pre class="literal-block">
-$ # run a demo app
+$ curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "yay!"}
</pre>
-<p>To describe a web service in <em>cornice</em>, you have to write something like this</p>
-<div class="system-message">
-<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 54)</p>
-<p>Content block expected for the "code-block" directive; none found.</p>
+<p>Validators can also attach extra information about validations to the request,
+using <cite>request.validated</cite>. It is a standard dict automatically attached to the
+requests.</p>
+<p>For instance, in our validator, we can chose to validate the parameter passed
+and use it in the body of the webservice:</p>
+<div class="highlight"><pre><span class="n">service</span> <span class="o">=</span> <span class="n">Service</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"service"</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s">"/service"</span><span class="p">)</span>
+
+
+<span class="k">def</span> <span class="nf">is_awesome</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">if</span> <span class="ow">not</span> <span class="s">'awesome'</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">'body'</span><span class="p">,</span> <span class="s">'awesome'</span><span class="p">,</span>
+ <span class="s">'the awesome parameter is required'</span><span class="p">)</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'awesome '</span> <span class="o">+</span> <span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]</span>
+
+
+<span class="nd">@service.get</span><span class="p">(</span><span class="n">validator</span><span class="o">=</span><span class="n">is_awesome</span><span class="p">)</span>
+<span class="k">def</span> <span class="nf">get1</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
+ <span class="k">return</span> <span class="p">{</span><span class="s">"test"</span><span class="p">:</span> <span class="n">request</span><span class="o">.</span><span class="n">validated</span><span class="p">[</span><span class="s">'awesome'</span><span class="p">]}</span>
+</pre></div>
<pre class="literal-block">
-.. code-block:: python
+curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "awesome yeah"}
+</pre>
+</div>
+<div class="section" id="dealing-with-accept-headers">
+<h2>Dealing with "Accept" headers</h2>
+<p>The HTTP spec defines a <strong>Accept</strong> header the client can send so the response
+is encoded the right way. A resource, available at an URL, can be available in
+different formats. This is especially true for web services.</p>
+<p>Cornice can help you to deal with this. The services you define can tell which
+content-types they can deal with, and this will be checked against the
+<strong>Accept</strong> headers sent by the client.</p>
+<p>Let's refine a bit our previous example, by specifying which content-types are
+supported, using the <cite>accept</cite> parameter:</p>
+<div class="system-message">
+<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">./content/mozilla/introducing-cornice.rst</tt>, line 117)</p>
+<p>Error in "code-block" directive:
+1 argument(s) required, 0 supplied.</p>
+<pre class="literal-block">
+.. code-block::
+
+ @service.get(validator=is_awesome, accept=("application/json", "text/json"))
+ def get1(request):
+ return {"test": "yay!"}
</pre>
</div>
+<p>Now, if you specifically ask for XML, for instance, cornice will throw a 406
+with the list of accepted content-types:</p>
+<pre class="literal-block">
+$ curl -vH "Accept: application/xml" http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: application/xml
+>
+< HTTP/1.0 406 Not Acceptable
+< Content-Type: application/json; charset=UTF-8
+< Content-Length: 33
+<
+["application/json", "text/json"]
+</pre>
+</div>
+<div class="section" id="building-your-documentation-automatically">
+<h2>Building your documentation automatically</h2>
+<p>XXX</p>
</div>
\ No newline at end of file
diff --git a/introducing-cornice.html b/introducing-cornice.html
index 3c1a3b2..c113980 100644
--- a/introducing-cornice.html
+++ b/introducing-cornice.html
@@ -29,12 +29,13 @@ it is easily pluggable with validations frameworks, such as Collander.
defis_awesome(request):ifnot'awesome'inrequest.GET:
- request.errors.add('body','awesome','You lack awesomeness!')
+ request.errors.add('body','awesome',
+ 'the awesome parameter is required')
-@service.get(validator=(is_awesome))
+@service.get(validator=is_awesome)defget1(request):
- return{"test":"succeeded"}
+ return{"test":"yay!"}
All the errors collected during the validation process, or after, are collected
before returning the request. If any, a error 400 is fired up, with the list of
@@ -45,19 +46,91 @@ formats in the future)
location is where the error arised. It can either be "body", "query", "headers"
or "path". name is the name of the variable causing problem, if any, and
description contains a more detailled message.
-
Here is an example of a malformed request:
+
Let's run this simple service, with bin/paster serve and send some queries to
+it:
+
$ curl -v http://127.0.0.1:5000/service
+> GET /service HTTP/1.1
+> Host: 127.0.0.1:5000
+> Accept: */*
+>
+* HTTP 1.0, assume close after body
+< HTTP/1.0 400 Bad Request
+< Content-Type: application/json; charset=UTF-8
+[{"location": "body", "name": "awesome", "description": "You lack awesomeness!"}
+
+
I've removed the extra clutter from the curl's output, but you got the general idea.
+
The content returned is in JSON, and I know exactly what I have to do: add an
+"awesome" parameter in my query. Let's do it again:
-$ # run a demo app
+$ curl http://127.0.0.1:5000/service?awesome=yeah
+{"test": "yay!"}
-
To describe a web service in cornice, you have to write something like this
-
-
System Message: ERROR/3 (./content/mozilla/introducing-cornice.rst, line 54)
-
Content block expected for the "code-block" directive; none found.
+
Validators can also attach extra information about validations to the request,
+using request.validated. It is a standard dict automatically attached to the
+requests.
+
For instance, in our validator, we can chose to validate the parameter passed
+and use it in the body of the webservice:
The HTTP spec defines a Accept header the client can send so the response
+is encoded the right way. A resource, available at an URL, can be available in
+different formats. This is especially true for web services.
+
Cornice can help you to deal with this. The services you define can tell which
+content-types they can deal with, and this will be checked against the
+Accept headers sent by the client.
+
Let's refine a bit our previous example, by specifying which content-types are
+supported, using the accept parameter:
+
+
System Message: ERROR/3 (./content/mozilla/introducing-cornice.rst, line 117)
+
Error in "code-block" directive:
+1 argument(s) required, 0 supplied.