-1

I am new to creating API's in cherrypy.So,I am just making a simple API to add numbers and display the answer.

I used this :

    import cherrypy
    import json
    def add(b,c):
        return a+b
    class addition:
         exposed = True


    def POST(self,c,d):
         result=add(c,d)
         return('addition result is' % result)


if __name__=='__main__':
     cherrypy.tree.mount(
         addition(),'/api/add',
         {'/':
             {'request.dispatch':cherrypy.dispatch.MethodDispatcher()}
         }
         )
 cherrypy.engine.start()

and then on a different file,to call the POST method,I wrote this code:

 import requests
 import json
 url = "http://127.0.0.1:8080/api/add/"
 data={'c':12,
      'd':13
      }
 payload = json.dumps(data)

 headers = {'content-type':"application/json"}

 response = requests.request("POST",url,data=payload,headers=headers)

 print(response.text)

but I am getting an error :HTTPError: (404, 'Missing parameters: c,d')

Please may I know where I am going wrong.

dwich
  • 1,710
  • 1
  • 15
  • 20

1 Answers1

0

First start without JSON to make sure your code works. Then update your code to work with JSON. Here is a working example of your calculator.

This example shows it without JSON. Example with JSON is below.

Working example without JSON

Server side in cherrypy (server1.py):

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cherrypy


def add(a, b):
    return a + b

class Calculator:
    exposed = True
    def POST(self, a, b):
        result = add(int(a), int(b))
        return 'result is {}'.format(result)


if __name__=='__main__':
    app_config = {
        '/': {'request.dispatch':cherrypy.dispatch.MethodDispatcher()}
    }
    cherrypy.tree.mount(Calculator(), '/api/add', config=app_config)

    cherrypy.config.update({
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
    })
    cherrypy.engine.start()
    cherrypy.engine.block()

Run the cherrypy server as a separate process. Open another terminal window, command line or whatever you use and use this API client to send a request. Name it client1.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests

url = 'http://127.0.0.1:9090/api/add'
data = {
    'a': 12,
    'b': 13
}

response = requests.request('POST', url, data=data)
print(response.text)

If you send the request, you get this response:

$ python send.py
result is 25

Working example with JSON

CherryPy server side (server2.py):

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import cherrypy
import json


def add(a, b):
    return a + b

class Calculator:
    exposed = True
    @cherrypy.tools.json_in()
    def POST(self):
        input_json = cherrypy.request.json
        result = add(input_json['a'], input_json['b'])
        return 'result is {}'.format(result)


if __name__=='__main__':
    app_config = {
        '/': {'request.dispatch':cherrypy.dispatch.MethodDispatcher()}
    }
    cherrypy.tree.mount(Calculator(), '/api/add', config=app_config)

    cherrypy.config.update({
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
    })
    cherrypy.engine.start()
    cherrypy.engine.block()

API client, name it client2.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import requests

url = 'http://127.0.0.1:9090/api/add'
data = {
    'a': 12,
    'b': 13
}

headers = {'content-type': 'application/json'}

# Note that there is json=data not data=data.
# This automatically converts input data to JSON
r = requests.request('POST', url, json=data, headers=headers)

print(r.status_code)
print(r.text)
dwich
  • 1,710
  • 1
  • 15
  • 20