1

I need to do a GET request to my Django server through an android application. The problem is that this GET function is written in Django with a @login_required.

What kind of request should i do to login and perform the GET request?

My API:

from django.shortcuts import render
from django.http import HttpResponse,HttpResponseServerError, HttpResponseBadRequest
from rest_framework.decorators import api_view
from .models import Order, New
import json
from django.shortcuts import render_to_response
from .forms import NewForm
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.decorators import login_required
from .validation import *
from django.views.decorators.csrf import csrf_exempt
@login_required
@api_view(['GET'])
def get_order(request):

    order_list = Order.objects.values("user_name",
                        "user_surname",
                        "order_date").all()

    return HttpResponse(json.dumps([x for x in order_list])

My urls.py:

from django.urls import path
from django.conf.urls import url
from . import views

app_name = 'API'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^order', views.order, name='order'),
    url(r'^get_order', views.get_order, name='get_order')]

and:

from django.contrib import admin
from . import settings
from django.views.static import serve
from django.urls import path, include, re_path
from django.conf.urls.static import static
from API.views import index

urlpatterns = [
    path('API/', include('API.urls',namespace="API")),
    path('admin/', admin.site.urls),
    path('', index, name='index'),
    path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})
]

In Postman i'm performing a GET (http://127.0.0.1:8000/API/get_order) with basic auth, receiving 302 status code.

1
  • 1
    maybe you need to build RESTFul API with django-rest-framework. There are several approaches for client authentication in DRF, eg. TokenAuthentication. If you don't want to build RESTFul API, you need to leverage the WebView to access your django website instead.
    – Enix
    Sep 19, 2018 at 3:09

1 Answer 1

1

I'm not sure how this can be done in Android side, but the basic idea is that you should add authentication credentials in your GET request.

I'll show you an example of the same by using Python's Requests library

from requests.auth import HTTPBasicAuth
import requests

requests.get('http://my/url/end/poin/', auth=HTTPBasicAuth('my_username', 'mypass'))



May be these results will help you to find the answer ;)


UPDATE

1. go to Authorization tab
2. choose Basic Auth type
3. Provide your username and password in respective text boxes
4. hit the send button

enter image description here

UPDATE-2
change your view as below

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response


@permission_classes((IsAuthenticated,))
@api_view(['GET'])
def get_order(request):
    order_list = Order.objects.values("user_name",
                                      "user_surname",
                                      "order_date").all()

    return Response(data=[x for x in order_list])
10
  • Thank you but unfortunatly i got status error HTTP/1.1" 302 0
    – Dev9977
    Sep 19, 2018 at 16:22
  • Pls do add the corresponding view to the question
    – JPG
    Sep 19, 2018 at 16:22
  • Seems like you are mixing plain Dango views with DRF views. Pls add the imports statement also
    – JPG
    Sep 19, 2018 at 16:41
  • And, add your urls.py tooo
    – JPG
    Sep 19, 2018 at 16:41
  • For testing purpos, comment out all url patterns other than path('API/'.....) and url('get_order'.....) and then try in Postman
    – JPG
    Sep 19, 2018 at 16:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.