From b1196a239b862c3d7ce7809dbd84f667cfc4c014 Mon Sep 17 00:00:00 2001 From: Steven Honson Date: Mon, 20 Aug 2012 21:54:27 +1000 Subject: [PATCH] Support for setting multiple cookies in a single request. --- AUTHORS | 3 ++- README.md | 1 + httpbin/core.py | 12 ++++++++++++ httpbin/templates/httpbin.1.html | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 2ca4276..0c4f6fc 100644 --- a/AUTHORS +++ b/AUTHORS @@ -15,4 +15,5 @@ Patches and Suggestions - Lispython - Kyle Conroy - Flavio Percoco -- Radomir Stevanovic (http://github.com/randomir) \ No newline at end of file +- Radomir Stevanovic (http://github.com/randomir) +- Steven Honson diff --git a/README.md b/README.md index 37251c8..0a1ab64 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Freely hosted in [HTTP](http://httpbin.org) & - [`/relative-redirect/:n`](http://httpbin.org/relative-redirect/6) 302 Relative redirects *n* times. - [`/cookies`](http://httpbin.org/cookies) Returns cookie data. - [`/cookies/set/:name/:value`](http://httpbin.org/cookies/set/key/value) Sets a simple cookie. +- [`/cookies/set?name=value`](http://httpbin.org/cookies/set?k1=v1&k2=v2) Sets one or more simple cookies. - [`/basic-auth/:user/:passwd`](http://httpbin.org/basic-auth/user/passwd) Challenges HTTPBasic Auth. - [`/hidden-basic-auth/:user/:passwd`](http://httpbin.org/hidden-basic-auth/user/passwd) 404'd BasicAuth. - [`/digest-auth/:qop/:user/:passwd`](http://httpbin.org/digest-auth/auth/user/passwd) Challenges HTTP Digest Auth. diff --git a/httpbin/core.py b/httpbin/core.py index 9bd9521..c13f339 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -259,6 +259,18 @@ def set_cookie(name, value): return r +@app.route('/cookies/set') +def set_cookies(): + """Sets cookie(s) as provided by the query string and redirects to cookie list.""" + + cookies = dict(request.args.items()) + r = app.make_response(redirect('/cookies')) + for key, value in cookies.items(): + r.set_cookie(key=key, value=value) + + return r + + @app.route('/basic-auth//') def basic_auth(user='user', passwd='passwd'): """Prompts the user for authorization using HTTP Basic Auth.""" diff --git a/httpbin/templates/httpbin.1.html b/httpbin/templates/httpbin.1.html index 5b3d2a4..182da11 100644 --- a/httpbin/templates/httpbin.1.html +++ b/httpbin/templates/httpbin.1.html @@ -21,6 +21,7 @@
  • /relative-redirect/:n 302 Relative redirects n times.
  • /cookies Returns cookie data.
  • /cookies/set/:name/:value Sets a simple cookie.
  • +
  • /cookies/set?name=value Sets one or more simple cookies.
  • /basic-auth/:user/:passwd Challenges HTTPBasic Auth.
  • /hidden-basic-auth/:user/:passwd 404'd BasicAuth.
  • /digest-auth/:qop/:user/:passwd Challenges HTTP Digest Auth.