Token Authentication Django Rest Framework HTTPie Error


(PARTH JANI) #1

Hello guys I am trying to test Token Authentication i have implemented with Django Rest Framework using httpie as per your tutorial .

My Command:
http http://127.0.0.1:8000/hello/ 'Authorization: Token 144d89221a3dcdefa40e2856c3916a598e7c35c7'

Getting this error :

usage: http [–json] [–form] [–pretty {all,colors,format,none}]
[–style STYLE] [–print WHAT] [–headers] [–body] [–verbose]
[–all] [–history-print WHAT] [–stream] [–output FILE]
[–download] [–continue]
[–session SESSION_NAME_OR_PATH | --session-read-only SESSION_NAME_OR_PATH]
[–auth USER[:PASS]] [–auth-type {basic,digest}]
[–proxy PROTOCOL:PROXY_URL] [–follow]
[–max-redirects MAX_REDIRECTS] [–timeout SECONDS]
[–check-status] [–verify VERIFY]
[–ssl {ssl2.3,tls1,tls1.1,tls1.2}] [–cert CERT]
[–cert-key CERT_KEY] [–ignore-stdin] [–help] [–version]
[–traceback] [–default-scheme DEFAULT_SCHEME] [–debug]
[METHOD] URL [REQUEST_ITEM [REQUEST_ITEM …]]
http: error: argument REQUEST_ITEM: “Token” is not a valid value

This is my VIEWS FILE

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated

class HelloView(APIView):
	permission_classes = (IsAuthenticated,)

	def get(self,request):
		content = {'message': 'Hello, World!'}
		return Response(content)

This is my URLS File

from django.contrib import admin
from django.urls import path
from core import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.HelloView.as_view(), name='hello'),
]

(Vitor Freitas) #2

Try double quotes instead

http http://127.0.0.1:8000/hello/ "Authorization: Token 144d89221a3dcdefa40e2856c3916a598e7c35c7"

(PARTH JANI) #3

Still getting http: error: argument REQUEST_ITEM: "Token" is not a valid value

I try it both

http http://127.0.0.1:8000/hello/ "Authorization: Token d4be4d411775230c1c8eed7b3bf9fb3e3a9510d9"

http http://127.0.0.1:8000/hello/ 'Authorization: Token d4be4d411775230c1c8eed7b3bf9fb3e3a9510d9'

I can also generate new token key but still not work.


(PARTH JANI) #4

Getting this new one error

http: error: ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /hello/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0262FD70>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it')) while doing GET request to URL: http://127.0.0.1:8000/hello/