0

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
4

5 回答 5

2

你可以尝试使用 curl 甚至 postman 来发出请求,看看它是否有效,

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

或者使用邮递员,在请求标头上传递您的令牌,如下所示:

在此处输入图像描述

不要忘记使用命令生成令牌

python manage.py drf_create_token YOURUSER
于 2018-12-05T10:16:20.777 回答
0

由于您使用的是 Windows,因此您应该使用双引号来包含Authorization标题。Windows(特别是 cmd.exe)不识别单引号并将标头的每个部分视为单独的参数。

所以改为:

http http://127.0.0.1:8000/hello/ "Authorization: Token c9a5b630b1887efdd3ca2db82aae9cec2c44257e"
于 2018-12-05T11:48:09.517 回答
0

解决方法很简单,如下。使用双引号代替单引号,这与DRF 文档所说的相反

对于 curl 使用下面的命令

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

对于 HTTPie 使用

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

请注意,在文档中使用双引号与单引号相反。

于 2018-12-05T11:54:15.530 回答
0

当我将 cmd 切换到 Windows 10 时,问题本身就解决了。bash。

于 2021-02-15T20:28:28.147 回答
0

你确定你已经为 http call 设置了标头吗?

--headers

这是来自 API 文档的示例:

curl -X GET http://127.0.0.1:8000/api/example/ -H 'Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
于 2018-12-05T09:58:08.020 回答