0

I'm working on a simple REST service with flask, the method deleteTweets() can't retrieve the URL parameter i'm sending, so tried to print to see what is sending but i get this error line.

  File "C:\Path\HomeController.py", line 26, in deleteTwee
ts
    id = request.args.get['id']
AttributeError: 'function' object has no attribute 'args'
127.0.0.1 - - [17/Jan/2018 12:00:05] "DELETE /api/deleteTweet?id=ABC HTTP/1.1" 500 

This the code:

import json
import Service
from flask import Flask, render_template,jsonify,request


app = Flask(__name__)

apiUrl='/api'


@app.route('/')
def hello_world():
    return render_template("/home.html", code=400)

@app.route(apiUrl+'/getData', methods=['GET'])
def test_request():
    list = [15,21,8]
    return jsonify(list)

@app.route(apiUrl+'/getTweets', methods=['GET'])
def request():
    return Service.getAlltwits()

@app.route(apiUrl+'/deleteTweet', methods=['DELETE'])
def deleteTweets():
    id = request.args.get['id'] 
    print(id)
    return 

It's very simple but not sure what i did missed. also tried with id = request.args.get('id')

Progs
  • 1,059
  • 7
  • 27
  • 63
  • 1
    Anytime you get an `AttributeError`, it helps to `print(request.__dict__)` and `print(dir(request))`. This will show you the possible attrs and methods of a given object. Then you don't have to guess about what is accessible. – JacobIRR Jan 17 '18 at 18:11
  • Also, `request` is a function, not an object. So you should call it if you plan on invoking its functionality: `request()` – JacobIRR Jan 17 '18 at 18:11
  • @JacobIRR That's odd since i based my code in this question: https://stackoverflow.com/questions/24892035/python-flask-how-to-get-parameters-from-a-url – Progs Jan 17 '18 at 18:14
  • 3
    Defining your own function named `request` overwrites the `from flask import request` you did at the top. – John Gordon Jan 17 '18 at 18:14
  • `@app.route(apiUrl+'/getTweets', methods=['GET']) def request(): return Service.getAlltwits()` you have function named "request", thats why you are getting "function' object has no attribute 'args'" – How about nope Jan 17 '18 at 18:15
  • Ah, so you have a name conflict. request vs request – JacobIRR Jan 17 '18 at 18:15

2 Answers2

4

You're doing the correct thing - and you're importing request.

But you have a function named request as well, and this function overwrites the former name (which is the actual flask request):

@app.route(apiUrl+'/getTweets', methods=['GET'])
def request():
    return Service.getAlltwits()

Change the name to get_tweets instead:

@app.route(apiUrl+'/getTweets', methods=['GET'])
def get_tweets():
    return Service.getAlltwits()
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
2

You've defined a function called request, which has therefore hidden the global request variable. Rename your route function.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895