Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1.  
  2.  
  3. I am trying to get determine if I am passing an object key value pair to a get request in a python rest api. I have the following:
  4.  
  5. @app.route('/', methods=['GET', 'DELETE', 'POST'])
  6. def profits():
  7.     if request.method=='GET':
  8.         if 'listname' in request.json:
  9.             print 'inside listname == True!!!'
  10.             conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "Fvnjty0b")
  11.             cur = conn.cursor()
  12.             sql = 'SELECT * FROM ledger WHERE listname = %s'
  13.             params = (request.json['listname'])
  14.             cur.execute(sql, params)
  15.             conn.commit()
  16.             data = cur.fetchall()
  17.             conn.close()
  18.             return jsonify(data)
  19.         else:
  20.             print 'inside listname == False!!!'
  21.             conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "Fvnjty0b")
  22.             cur = conn.cursor()
  23.             sql = 'SELECT * FROM ledger'
  24.             cur.execute(sql)
  25.             conn.commit()
  26.             data = cur.fetchall()
  27.             conn.close()
  28.             return jsonify(data)
  29.  
  30. Which I call on my front end with an axios call
  31.  
  32.   axios({
  33.           method: 'get',
  34.           url: 'http://localhost:5000/',
  35.           headers: {
  36.             'Content-type': 'application/json'
  37.             },
  38.           data:{
  39.             'listname': this.ledgername
  40.               }
  41.         })
  42.         .then((response)=>{
  43.           console.log('this is the response from the python server on a get request of listname ', response);
  44.         })
  45.         .catch(error=>{
  46.           console.log('here is the error on a get request from the python server of listname ', error);
  47.         })
  48.  
  49.  
  50. But the error I get is:
  51.  
  52.  
  53.  
  54.  
  55. Traceback (most recent call last):
  56.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
  57.     response = self.full_dispatch_request()
  58.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
  59.     rv = self.handle_user_exception(e)
  60.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  61.     return cors_after_request(app.make_response(f(*args, **kwargs)))
  62.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
  63.     reraise(exc_type, exc_value, tb)
  64.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
  65.     rv = self.dispatch_request()
  66.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
  67.     return self.view_functions[rule.endpoint](**req.view_args)
  68.   File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 50, in profits
  69.     if 'listname' in request.json:
  70.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/werkzeug/local.py", line 379, in <lambda>
  71.     __contains__ = lambda x, i: i in x._get_current_object()
  72. TypeError: argument of type 'Request' is not iterable
  73.  
  74.  
  75.  
  76.  
  77.  
  78. My google fu is failing me, but I suspect that python is somehow not allowed to look through the request list by looping through it. However I have seen other people on stackoverflow say that similar things will work. What am I not seeing?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement