indianLeo
indianLeo

Reputation: 153

AttributeError: 'WSGIRequest' object has no attribute 'get' while making a get request

I am trying to make a get request to stackoverflow API but I am facing a WSGI error while making the requests through POSTMAN. Here is the code snippet:

Views:

def search(requests):
    query = requests.GET["query"]
    response = requests.get('https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + query + '&site=stackoverflow')
    api_Data = response.json()
    return JsonResponse({"message": api_Data})

URLs:

urlpatterns = [
    path('search/', views.search, name='search'),
]

I have tried I keep getting 'WSGIRequest' object has no attribute 'Get' on django but it did not work.

Upvotes: 2

Views: 1200

Answers (1)

Steve Jalim
Steve Jalim

Reputation: 12195

You're running into trouble here because you (I am assuming) have imported python-requests as requests earlier in your file, but you are overriding that by naming the HTTPRequest object that your view get as requests too.

Here's a fixed version, by naming the argument to the view function simply as request (which is more common in Django, too)

import requests  # note the name

...

# variable name changed
def search(request):  

    # and changed here too, as well as allowing for "query" not to be present in `request.GET`
    query = request.GET.get("query")  

    response = requests.get('https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + query + '&site=stackoverflow')
    api_Data = response.json()
    return JsonResponse({"message": api_Data})

Upvotes: 3

Related Questions