Convert FORMSDICT bottle to Python dictionary (safe stream)

I just worked [bottle tutorial [ 1 and found the following useful table (I hope we get the format on the right) where you can access the types of request attributes

Attribute           GET Form fields     POST Form fields    File Uploads
BaseRequest.query   yes                 no                  no
BaseRequest.forms   no                  yes                 no
BaseRequest.files   no                  no                  yes
BaseRequest.params  yes                 yes                 no
BaseRequest.GET     yes                 no                  no
BaseRequest.POST    no                  yes                 yes

Of course, I want to try it myself, but since Bottle data structures are special thread-safe versions, and I wanted to use json to print in a reasonable format, I wrote the following (working) test program

from bottle import run, route, request, response, template, Bottle
import uuid
import json
import os

ondersoek = Bottle()

@ondersoek.get('/x')
@ondersoek.post('/x')
def show_everything():
   PythonDict={}
   PythonDict['forms']={}
   for item in request.forms:
      PythonDict['forms'][item]=request.forms.get(item)

   PythonDict['query']={}
   for item in request.forms:
      PythonDict['query'][item]=request.query.get(item)

   #The below does not work - multipart/form-data doesn't serialize in json
   #PythonDict['files']={}
   #for item in request.files:
      #PythonDict['files'][item]=request.files.get(item)

   PythonDict['GET']={}
   for item in request.GET:
      PythonDict['GET'][item]=request.GET.get(item)

   PythonDict['POST']={}
   for item in request.POST:
      PythonDict['POST'][item]=request.POST.get(item)

   PythonDict['params']={}
   for item in request.params:
      PythonDict['params'][item]=request.params.get(item)

   return json.dumps(PythonDict, indent=3)+"\n"

ondersoek.run(host='localhost', port=8080, reloader=True)

This works, I get:

tahaan@Komputer:~/Projects$ curl -G -d dd=dddd http://localhost:8080/x?q=qqq
{
   "files": {}, 
   "GET": {
      "q": "qqq", 
      "dd": "dddd"
   }, 
   "forms": {}, 
   "params": {
      "q": "qqq", 
      "dd": "dddd"
   }, 
   "query": {}, 
   "POST": {}
}

and

tahaan@Komputer:~/Projects$ curl -X POST -d dd=dddd http://localhost:8080/x?q=qqq
{
   "files": {}, 
   "GET": {
      "q": "qqq"
   }, 
   "forms": {
      "dd": "dddd"
   }, 
   "params": {
      "q": "qqq", 
      "dd": "dddd"
   }, 
   "query": {
      "dd": null
   }, 
   "POST": {
      "dd": "dddd"
   }
}

, , Bottle Python. - - , , CherryPy, - . , : "" dict , JSON ()? - FormsDict-To-Json?

+4
2

. I.e., , .

, - . ; , .

, Bottle docs ( ):

- BaseRequest . (). /, ( ).

, , request , Bottle "", - . ; , , . . , .


PythonDict : :

PythonDict={}

, dict , ; dict , . ( : .)

, dict; : . dict , .

, !

+1

, , , Bottle . , , .

0

All Articles