首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >授权头在浏览器的put请求中被删除,但在邮递员中工作良好。

授权头在浏览器的put请求中被删除,但在邮递员中工作良好。
EN

Stack Overflow用户
提问于 2020-05-15 04:11:11
回答 1查看 149关注 0票数 1

在从浏览器(使用axios的reactjs)到Django服务器的请求中删除授权令牌,从而获得401(未经授权的),但同样的请求与邮递员工作得很好。请求的代码

代码语言:javascript
复制
const config = {
        headers:{
           'Content-Type': 'application/json',
           'Authorization': `Token ${localStorage.token}`
        }
     }
    console.log(config.headers.Authorization)
    console.log(localStorage.token)

    axios.put('http://localhost:8000/user-registration/', config).then(
         res => {
            console.log(res.status)
         } 
      )
}

Djnago方法

代码语言:javascript
复制
class UserRegistrationEvent(APIView):
permission_classes = (IsAuthenticated,) 
authentication_classes = (TokenAuthentication, ) 
def get_object(self, username):
    try:
        return User.objects.get(username = username)
    except MyUser.DoesNotExist:
        raise Http404

def put(self, request, format=None):
    print(request.headers)
    User       = self.get_object(request.user)
    serializer = UserRegistrationEventSerializer(User, data=request.headers)
    if serializer.is_valid():
        serializer.save()
        return Response({'alert': 'registration successful'})
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

与方法中的方法一样,我使用print方法查找标头(为了调试,我删除了权限类)。CORS设置不是问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-15 04:15:32

将选项/config作为第三个参数传递给axios.put。第二个参数是数据/有效载荷。

像这样的

代码语言:javascript
复制
axios.put('http://localhost:8000/user-registration/', {}, config).then(
         res => {
            console.log(res.status)
         } 
      )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61811629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档