Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rest api parameter in flask
#1
Hi

How can I specify a parameter to task 1.

I have a task list, which is triggered by an integer (1 or 2). But in addition to the integer, I would like to pass in additional parameters, not necessarily number types.

This is what I currently have:

#!/usr/bin/python3
from flask import Flask, jsonify, json
import cx_Oracle

con = cx_Oracle.connect('ordb/ordb@//10.11.12.18:1521/FTNODE')

app = Flask(__name__)

tasks = [
    {
        'id': 1,
        'title': 'Customer list',
        'description': 'List of customers currently in the database', 
        'done': False
    },
    {
        'id': 2,
        'title': 'Warehouse List',
        'description': 'List of warehouses currently in the database', 
        'done': False
    }
]

@app.route('/warehouse/api/v1.0/api', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

@app.route('/warehouse/api/v1.0/api/<int:task_id>', methods=['GET'])
def get_task(task_id):
    task = [task for task in tasks if task['id'] == task_id]
    if len(task) == 0:
      abort(404)

    print(task_id)
    if task_id==1:
      c = con.cursor()
      query = 'select * from customer'
      result = list(c.execute(query))
      c.close()
      jsonData = json.dumps(result)

    print(task_id)
    if task_id==2:
      c = con.cursor()
      query = 'select * from warehouse'
      result = list(c.execute(query))
      c.close()
      jsonData = json.dumps(result)

    return jsonify({'task': jsonData})

if __name__ == '__main__':
    app.run(port=5001,debug=False)  
    con.close()
Sample output:

$ curl -i http://127.0.0.1:5001/warehouse/api/v1.0/api/1
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 332
Server: Werkzeug/0.14.1 Python/3.6.5
Date: Thu, 21 Jun 2018 13:41:30 GMT

{"task":"[[41, \"Aaron\", \"A.\", \"Aaronson\"], [42, \"Diana\", null, \"Birch\"], [43, \"David\", \"C.\", \"Pack\"], [44, \"Hakim\", null, \"Dogan\"], [45, \"Anita\", null, \"Eaton\"], [46, \"Jasmin\", null, \"Faraday\"], [47, \"Gene\", \"B.\", \"McClene\"], [48, \"Todd\", null, \"Howard\"], [49, \"Irma\", null, \"Howitzer\"]]"}
Reply
#2
get_task is just a function. You can pass any arguments you want to it along with the task id.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Perhaps I should've been somewhat clearer.
My question related to the url.

Could I for example have something like this?
@app.route('/warehouse/api/v1.0/api/<int:task_id>/<string:some_value>', methods=['GET'])
In other words, what types can I have as arguments ?
Reply
#4
(Jun-21-2018, 03:57 PM)bluefrog Wrote: In other words, what types can I have as arguments ?
Variable Rules
Can make a quick test like this.
As it are now only float works.
from flask import Flask

app = Flask(__name__)
# @app.route('/hello/<name>') # Default string
# @app.route('/hello/<int:name>')
@app.route('/hello/<float:name>')
def hello(name):
    print(type(name))
    return f'Hello {name}'

if __name__ == '__main__':
    app.run(debug=True)
Output:
http://127.0.0.1:5000/hello/3.14 <class 'float'> # printed to shell Hello 3.14
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,654 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  API REST Package for Calling/Flask muzikman 12 4,138 Oct-22-2021, 07:51 PM
Last Post: muzikman
  Help with return on REST API Flask korenron 0 1,286 Jun-13-2021, 10:40 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020