0

I have created a table employee_details as below and then with two api's to add a new user and other to show all users as:

from flask import Flask,jsonify,request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/empdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db=SQLAlchemy(app)
ma = Marshmallow(app)


class employee_details(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    empid = db.Column(db.Integer)
    ename = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    city = db.Column(db.String(120), unique=True, nullable=False)
    department = db.Column(db.String(120), unique=True, nullable=False)

    def __init__(self,empid, ename, email,city,department):
        self.ename = ename
        self.email = email
        self.empid=empid
        self.city=city
        self.department=department

class EmpSchema(ma.Schema):
    class Meta:
        fields = ('ename', 'empid','city','department')

emp_schema = EmpSchema()
emps_schema = EmpSchema(many=True)


db.create_all()

@app.route("/emp", methods=["POST"])
def add_emp():
    empid=request.json['empid']
    ename = request.json['ename']
    email = request.json['email']
    city=request.json['city']
    department=request.json['department']


    new_emp = employee_details(empid,ename, email,city,department)


    db.session.add(new_emp)
    db.session.commit()

    return emp_schema.jsonify(new_emp)

@app.route('/emp',methods=['GET'])
def show_emps():
    all_emps=employee_details.query.all()
    result=emps_schema.dump(all_emps)
    return jsonify(result)


if __name__=='__main__':
    app.run(debug=False)

How can write another api to show that particular employee detail based on email in json(email sent from POSTMAN not a form)? I tried as :

@app.route('/getemp',methods=['GET','POST'])
def get_emp():
    email=request.json['email'] #trying to get email from POST (from postman)
    emp=employee_details.query.filter_by(email=email).first() #filter and get whole details
    return jsonify(emp)

which doesn't work and I guess its wrong way!! any help for this noob question is appreciated

Codenewbie
  • 1,106
  • 7
  • 21

1 Answers1

1

POST method isn't intended for getting resources. Use GET instead.

You also need to use your EmpSchema so that the instance is dumped to dict

@app.route('/getemp',methods=['GET'])
def get_emp():
    email=request.args.get('email', '')
    emp=employee_details.query.filter_by(email=email).first() #filter and get whole details
    if not emp:
        return jsonify({}), 404
    return jsonify(emp_schema.dump(emp))

You can get your employee by entering /getemp?email=<your employee email>

stasiekz
  • 1,286
  • 3
  • 17
  • @app.route('/getemp',methods=['GET','POST']) def get_emp(): email=request.json['email'] emp=employee_details.query.filter_by(email=email).first() return emp_schema.jsonify(emp).....I tried this and I got , is this not correct way?,,....as your answer suggests to send email in url itself, not from the body through postman which I was thinking about mate – Codenewbie Sep 22 '19 at 06:16
  • also please can you explain this line....email=request.args.get('email', '').......why a place holder type after 'email',''?? – Codenewbie Sep 22 '19 at 06:21
  • 1
    Second argument to get is a default one, If it is not provided get returns None which will probably cause an error while trying to filter email. – stasiekz Sep 22 '19 at 07:00
  • and what I have tried is wrong way? as in above comment – Codenewbie Sep 22 '19 at 09:51
  • 1
    @Codenewbie As I said in the answer `POST` is inappropriate HTTP method for resources retrieval. Have a look at this explanation https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – stasiekz Sep 22 '19 at 11:22
  • can we hide email value here ::/getemp?email= after submitting "GET /login?email=ex@gmail.comHTTP/1.1" 200 -...? – Codenewbie Sep 23 '19 at 10:14