-1

I am working with CherryPy framework. I am implementing REST and what i want the HTTP Method GET to retrieve some information given a parameter passed in the URL that should be an integer. This parameter is optional and if not given it must be set by default to 10. In the CherryPy docs (https://docs.cherrypy.dev/en/latest/advanced.html) i found this piece of code in which where a default value for the variable length is set.

import random
import string

import cherrypy

class StringGenerator(object):
    @cherrypy.expose(['generer', 'generar'])
    def generate(self, length=8):
        return ''.join(random.sample(string.hexdigits, int(length)))

if __name__ == '__main__':
   cherrypy.quickstart(StringGenerator())

How can i do the same for GET?

1 Answers1

0

This should do the trick. your route i.e. /GET/ or /GET/122

@cherrypy.expose
def GET(self, id = 10):
    print(int(id))
    a = int(id)
    b = a + 100
    return str(b)
Igor
  • 298
  • 3
  • 8