2

This is outputI am trying to fetch data from the api endpoint using HTTPIE, following is the command,

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

I generated the token and appended it to the api endpoint, i used curl to fetch data too, but it shows similar error.

Views.py

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'}
     return Response(content)

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'rest_framework',
    'rest_framework.authtoken',
]

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': [
       'rest_framework.authentication.TokenAuthentication',
       'rest_framework.authentication.SessionAuthentication',
   ],
}

Output

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,ssl3,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
2
  • There's nothing wrong with your httpie command. What shell are you using to run the command? Bash? Commented Dec 5, 2018 at 10:51
  • I am executing on windows cmd Commented Dec 5, 2018 at 11:34

6 Answers 6

2

You can try to use curl or even postman to make the request and see if it works,

curl -X GET http://127.0.0.1:8000/hello/ -H "Authorization: Token Your generated token here"

Or using postman, pass your token on the request header like this:

enter image description here

Don't forget to generate a token using the command

python manage.py drf_create_token YOURUSER
Sign up to request clarification or add additional context in comments.

3 Comments

this is what i am getting: curl -X GET 127.0.0.1:8000/hello -H 'Authorization: Token c9a5b630b1887efdd3ca2db82aae9cec2c44257e' {"detail":"Authentication credentials were not provided."}curl: (6) Could not resolve host: Token curl: (6) Could not resolve host: c9a5b630b1887efdd3ca2db82aae9cec2c44257e'
try with double quotes "" instead of ''
Try what Mr-Programs said.
1

The solution is simple as is as follows . Use double quotes in the place of single quotes contrary to what the DRF Documentation says

For curl use the command below

curl -H "Authorization: Token c9a5b630b1887efdd3ca2db82aae9cec2c44257e" http://127.0.0.1:8000/hello/

For HTTPie use

http GET http://127.0.0.1:8000/hello/ "Authorization: Token c9a5b630b1887efdd3ca2db82aae9cec2c44257e"

Note that Double quotes are used contrary to single quotes in the documentation.

Comments

1

Since you're using Windows, you should use double quotes to contain the Authorization header. Windows (specifically cmd.exe) doesn't recognize the single quotes and treats each part of the header as a separate argument.

So change to:

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

Comments

0

Are you sure that you settled headers for http call ?

--headers

It's an example from API doc:

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'

Comments

0

The problem itself resolved when I switched cmd to windows 10. bash.

Comments

0

Double vs single quotes in curl command was the fix for me. I was using single and getting auth problems. Double quotes worked.

Comments