【问题标题】:getting code 400 message Bad request syntax , after post from flutter从颤振发布后,获取代码 400 消息错误的请求语法
【发布时间】:2021-08-29 12:29:19
【问题描述】:

获取代码 400 消息错误的请求语法,从颤振发布后,

使用邮递员请求发送并没有问题,但在将地图数据发布到 Django 服务器后出现抖动,我收到此错误


[19/May/2020 14:58:13] "POST /account/login/ HTTP/1.1" 406 42
[19/May/2020 14:58:13] code 400, message Bad request syntax ('32')
[19/May/2020 14:58:13] "32" 400 -

姜戈

@api_view(['POST'])
def login_user(request):
    print(request.data)
    if request.method == 'POST':
        response = request.data
        username = response.get('username')
        password = response.get('password')
        if password is not None and username is not None:
            user = authenticate(username=username, password=password)
            if user is not None:
                create_or_update_token = Token.objects.update_or_create(user=user)
                user_token = Token.objects.get(user=user)
                return Response({'type': True, 'token': user_token.key, 'username': user.username},
                                status=status.HTTP_200_OK)
            else:
                return Response({'type': False, 'message': 'User Or Password Incorrect'},
                                status=status.HTTP_404_NOT_FOUND)
        else:
            return Response({'type': False, 'message': 'wrong parameter'}, status=status.HTTP_406_NOT_ACCEPTABLE)
    else:
        return Response({'type': False, 'message': 'method is wrong'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)

颤抖

Future<dynamic> postGoa(String endpoint, Map data)async{
    Map map = {
      "username":"user",
      "password":"password"
    };
    var url = _getUrl("POST", endpoint);
    var client = new HttpClient();
    HttpClientRequest request = await client.postUrl(Uri.parse(url));
    request.headers.set('content-type', 'application/json');
    request.headers.set('Authorization', 'Bearer '+ athenticated
    );
    request.add(utf8.encode(json.encode(map)));
    HttpClientResponse response = await request.close();
    String mydata= await response.transform(utf8.decoder).join();
    client.close();

    return mydata;
  }

}


添加后

request.add(utf8.encode(json.encode(map)));


我在 Django 控制台中遇到错误

【问题讨论】:

  • 你有什么理由在 pub.dev 上使用 HttpClient 而不是 http 包吗?
  • 更新显示邮递员请求页面的问题。
  • 你有什么理由使用 HttpClient 而不是 pub.dev 上的 http 包?飞镖:io
  • curl --location --request POST '192.168.1.105/account/login' \ --header '授权:Bearer mona_krp' \ --header 'Content-Type: application/json' \ --data-raw '{ "username":"mrhaydari", "password":"PowerM@@2024" }' 发帖人请求
  • 你有什么理由使用 HttpClient 而不是 pub.dev 上的 http 包?飞镖:io

标签: django flutter


【解决方案1】:

尝试从 Django 应用程序中打印出您的请求标头。

print(request.headers)

我敢打赌其中一个标题是Content-Type: ''。如果是这种情况,Django 不会读取您的 POST 数据,因为它认为没有数据。我建议计算您在 Flutter 中发送的内容的长度,然后在您的请求中发送正确的 Content-Length 标头。

这可能看起来像这样(在您的 Flutter 应用中):

encodedData = jsonEncode(data); // jsonEncode is part of the dart:convert package
request.headers.add(HttpHeaders.contentLengthHeader, encodedData.length);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多