We have been using res.text in all the examples,
and now we will discover what other methods we can call on
res.
Nous avons utilisé res.text dans tous les exemples,
et maintenant nous allons découvrir quelles autres méthodes nous
pouvons appeler dans res.
We can get or set the status code of the response by using the
accessor res.status:
Nous pouvons obtenir ou mettre en place les codes des statuts de
la réponse en utilisant l'accesseur res.status:
require "syro" App = Syro.new do get do res.status = 501 res.text "Not implemented yet!" end end env = do "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[501,
{"Content-Length"=>"20"},
["Not implemented yet!"]]
You may have noticed the Content-Type header included
in all the responses. So far, its value has always been set to
"text/plain".
Vous avez sûrement remarqué l'entête Content-Type
inclus dans toutes les réponses. Jusqu'ici, sa valeur a toujours
été "text/plain".
The res.text method not only writes the body, but
also sets the "Content-Type" and
"Content-Length" headers.
La méthode res.text non seulement écrit le corps,
mais configure en plus les entêtes de "Content-Type"
et de "Content-Length".
Here are some alternative methods for writing the response body:
Voici quelques méthodes alternatives pour écrire la réponse du corps :
res.write "Hello"
The res.write method doesn't alter the
"Content-Type" header.
La méthode res.write n'altère pas l'entête
"Content-Type".
res.html "Hello"
The res.html method sets the "Content-Type"
header to "text/html".
La méthode res.html configure l'entête
"Content-Type" à "text/html".
res.json "Hello"
The res.json method sets the "Content-Type"
header to "application/json".
La méthode res.json configure l'entête
"Content-Type" à "application/json".
Note that aside from writing the response body and setting the
value for the Content-Type header, no encoding or serialization
takes place. If you want to return a JSON encoded response, make
sure to encode the objects yourself (i.e., res.json
JSON.dump(...)).
Notez que, mis à part l'écriture de la réponse du corps et la
configuration de la valeur pour l'entête Content-Type, aucun
encodage ou sérialisation n'aura lieu. Si vous voulez retourner
une réponse encodée JSON, assurez-vous d'encoder vous mêmes les
objets (ex. res.json JSON.dump(...)).
We can configure response headers with the res.headers
hash:
Nous pouvons configurer des entêtes de réponse avec le hash
res.headers :
require "syro" App = Syro.new do get do res.headers["Content-Type"] = "text/markdown" res.write "hello _you_" end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[200,
{"Content-Type"=>"text/markdown",
"Content-Length"=>"11"},
["hello _you_"]]
You can call res.redirect to take the visitor to a
different URL:
Vous pouvez appeler res.redirect pour amener le
visiteur vers une URL différente :
require "syro" App = Syro.new do get do res.redirect "/foo" end on "foo" do get do res.text "GET /foo" end end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[302,
{"Location"=>"/foo"},
[]]
Our little application is returning a redirection, which tells browsers to go to a different URL. Soon we will be able to see this result in the browser and it will all make sense.
Notre petite application retournera une redirection qui dira au navigateur d'aller vers une URL différente. Bientôt, nous serons capables de voir ce résultat dans le navigateur et tout cela aura du sens.
Manage cookies with res.set_cookie and
res.delete_cookie:
Gérez les cookies avec res.set_cookie et
res.delete_cookie:
require "syro" App = Syro.new do get do res.set_cookie("foo", "bar") res.redirect "/admin" end on "admin" do get do res.delete_cookie("foo") res.text req.cookies["foo"] end end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[302,
{"Set-Cookie"=>"foo=bar",
"Location"=>"/admin"},
[]]
We got a redirection and a cookie header. We can now simulate what the browser will do in order to follow the redirect. Append the code below:
Nous avons obtenu une redirection et un entête cookie. Maintenant nous pouvons simuler ce que le navigateur ferait pour suivre la redirection. Ajoutez le code ci-dessous :
env = {
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/admin",
"HTTP_COOKIE" => "foo=bar",
}
p App.call(env)
Output:
Résultat :
[200,
{"Set-Cookie"=>"foo=; ...",
"Content-Length"=>"3"},
"Content-Type"=>"text/plain"},
["bar"]]
Part of Set-Cookie was omitted in the example, but
it has to do with expiring the foo cookie.
Une partie du Set-Cookie a été omise dans l'exemple,
mais ce code sert à dire que le cookie foo est expiré.