rest api in cherrypy

this is my test server code ....

import cherrypy
import json

class HelloWorld(object):
    @cherrypy.expose
    def index(self):
        return "Hello World!"


    @cherrypy.expose
    @cherrypy.tools.json_out()
    @cherrypy.tools.json_in()
    def json(self):
        result = {"operation": "request", "result": "success"}
        if cherrypy.request.method == "POST":
           return "\n\nJson\n\n"+str(cherrypy.request.json)+"\n\n\n"


    @cherrypy.expose
    def param(self,name,age):
        return "\n\nParam\n\n\n{} is {} years old\n\n".format(name,age)


cherrypy.quickstart(HelloWorld())

Static JSON File

{
  "name":"tim",
  "age":"21"
}

Makefile

param:
        curl -d "name=tim&age=21" -X POST http://localhost:8080/param

param2:
        curl -d "name=shamsa&age=50" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:8080/param


json:
        curl -d '{"name":"tim", "age":"21"}' -H "Content-Type: application/json" -X POST http://localhost:8080/json

json2:
        curl -d "@json2.data" -X POST http://localhost:8080/json  -H "Content-Type: application/json"

Command Line Vars

We need a bash function

#!/bin/bash

#Create my vars I want to use
age="21"
name="tim"

#Create a BASH function
function gen()
{
cat <<  EOF
{"name":"$name",
"age":"$age"
}
EOF
}

So when we execute this we see

gen
{"name":"tim",
"age":"21"
}

Now the command line to CURL looks like this....

curl -X POST http://localhost:8080/json -H "Content-Type: application/json" --data "$(gen)"

Using Var with Bash Function

lady="Omaima"
curl -H "Content-Type: application/json" -X POST http://localhost:8080/json --data "$(name=$lady && age=23 && shamsa_gen)"