简体   繁体   English

Django 中的 Jsonresponse 在浏览器中工作,但不在 PostMan 或 Angular 中

[英]Jsonresponse in Django working in browser but not in PostMan or Angular

I am trying to send a JSON response from Django back-end to my angular front-end.我正在尝试从 Django 后端向我的 angular 前端发送 JSON 响应。

When I make the request I receive nothing in Postman or Angular but,opening the link in browser seems to be returning the correct result当我发出请求时,我在 Postman 或 Angular 中什么也没有收到,但是,在浏览器中打开链接似乎返回了正确的结果

My View is:我的观点是:

@api_view(['GET'])
def my_view(request):
    print(request.user.username)
    return JsonResponse({'username': request.user.username})

When I open http://127.0.0.1:8000/accounts/get_username/ in browser I receive {"username": "aditya8010"} on the web page.当我在浏览器中打开http://127.0.0.1:8000/accounts/get_username/时,我在 web 页面上收到{"username": "aditya8010"} But when i do a get request using POSTMAN I recieve但是当我使用 POSTMAN 发出请求时,我收到了

{
    "username": ""
}

Same with Angular与 Angular 相同

this.http.get("http://127.0.0.1:8000/accounts/get_username/").subscribe((res) => {
      
      this.username = JSON.stringify(res["username"])
      console.log(this.username,"  ", res)
    })

this code also prints an empty username string.此代码还打印一个空的用户名字符串。

Another thing I have noticed is that my print statement in the view does print anything random I put in there when called from POSTMAN or Browser but when I use request.user.username it doesnt print anything when called by POSTMAN.我注意到的另一件事是,当我从 POSTMAN 或浏览器调用时,我在视图中的打印语句确实打印了我放在那里的任何随机内容,但是当我使用 request.user.username 时,它在被 POSTMAN 调用时不打印任何内容。 And each time the response code is 200而且每次响应码都是200

What am I doing wrong.我究竟做错了什么。

When you're sending the request you are not providing authentication credentials (ie something that identifies the user that is sending the request).当您发送请求时,您没有提供身份验证凭据(即识别发送请求的用户的东西)。 How do you obtain this credentials?您如何获得此凭证?

You need to establish an authentication method.您需要建立一种身份验证方法。 There are several but I recommend using Token authentication with knox package .有几个,但我建议使用带有knox package的令牌身份验证。 Basically, you have an endpoint that logins the user with his username and password (normal json post request) and that endpoint returns a token.基本上,您有一个使用用户名和密码登录用户的端点(正常的 json 发布请求)并且该端点返回一个令牌。 This token is what identifies the user.此令牌用于识别用户。 You send this token in the header of each request you need to be authenticated.您在需要验证的每个请求的 header 中发送此令牌。 That means you probably should include an IsAuthenticated permission for the view.这意味着您可能应该为视图包含 IsAuthenticated 权限。 In postman:在 postman 中: 邮递员授权标头

API view: API 查看:

from rest_framework.permissions import IsAuthenticated

@api_view(['GET'])
@authentication_classes([IsAuthenticated])
def my_view(request):
    print(request.user.username)
    return JsonResponse({'username': request.user.username})

When it is in a browser, your login information is remembered in the session.当它在浏览器中时,您的登录信息会被记住在 session 中。 When using postman or Angular, you need to provide the user's information in the request header manually.使用 postman 或 Angular 时,需要在请求 header 中手动提供用户信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM