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')

6
  • 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. Commented Jan 17, 2018 at 18:11
  • Also, request is a function, not an object. So you should call it if you plan on invoking its functionality: request() Commented Jan 17, 2018 at 18:11
  • @JacobIRR That's odd since i based my code in this question: stackoverflow.com/questions/24892035/… Commented Jan 17, 2018 at 18:14
  • 3
    Defining your own function named request overwrites the from flask import request you did at the top. Commented Jan 17, 2018 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'" Commented Jan 17, 2018 at 18:15

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

ooh i feel dumb that i didn't notice that
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.