【问题标题】:how to send CSRF token using flutter http request如何使用颤振 http 请求发送 CSRF 令牌
【发布时间】:2021-12-10 13:41:42
【问题描述】:

如何使用 Flutter HTTP 请求发送 CSRF 令牌 我有一个基于 Django 的项目,它使用 django-rest-framework 作为 API,当我使用 Postman 发送 POST 请求时,它工作得非常好,但是当我从我的颤振应用程序发送 HTTP.post 请求时,我得到了这个响应:

Forbidden 403
CSRF verification failed. Request aborted.
You are seeing this message because this HTTPS site requires a “Referer header” to be sent by your Web browser, but none was sent

在 django 中使用function based view 接收请求:

@api_view(['POST'])
@permission_classes([AllowAny,])
@csrf_exempt
def create_user(request):
   ......
   .....

然后在URLS

    path("api/v1/create_user/", api.create_user, name="create_user"),

并且正在颤振中发送请求:

http.post(Uri(myURL),header={
    'Content-Type': 'application/x-www-form-urlencoded',
}
,body={
'my_key':'my_value',
})

【问题讨论】:

  • 你可以在header上传递它
  • @ArbiterChil 我什至可以从哪里得到它!如何传入标头,可以给我更多信息吗?
  • 只需传递带有“Referer”键的标头,值是任何 url
  • 不,它没有任何区别,同样的 403 禁止响应!

标签: python django flutter dart django-rest-framework


【解决方案1】:

使用 django 创建 HTTPS 网站时,需要使用 Referer 标头。 假设您网站的域是yoursite.com,您需要在标题中设置它

{"Referer": "https://yoursite.com"}

在飞镖/颤振中使用http package

import 'package:http/http.dart' as http;
var resp = http.post(Uri.parse("https://yoursite.com/api/v1/create_user"),
    headers: {"Referer": "https://yoursite.com"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2018-06-09
    • 2022-01-06
    • 2022-01-06
    • 2012-11-04
    相关资源
    最近更新 更多