Get Request 在邮递员中有效,但在 flutter 中无效,给出 400 错误

问题描述 投票:0回答:1

我正在发送一个带有 JSON 主体和不记名令牌的 get 请求,它在 Postman 中工作正常,但在 Flutter 代码中不起作用,并通过以下响应给出 400 个错误请求:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-2460b88e437fb2021b6249f0a3d0abf0-3f2e0cef2e5fefa3-00",
    "errors": {
        "$": [
            "The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
        ],
        "signIn": [
            "The signIn field is required."
        ]
    }
}

这是我的颤振代码:

final body = userData.toJsonLogin();

var headersList = {
  'Accept': '*/*',
  'Authorization': 'Bearer $token',
  'Content-Type': 'application/json',
};

var request =
    http.Request('GET', Uri.parse('https://192.168.0.128/api/Profile'));

request.body = body;

request.headers.addAll(headersList);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
} else {
  print(response.reasonPhrase);
}

如何解决这个问题 API 是我学长提供的,我无法更改。

flutter dart https postman http-status-code-400
1个回答
0
投票

我认为问题出在标题地图的标题中

var headersList = {
 'Accept': '*/*',
 'Authorization': 'Bearer $token',
 'Content-Type': 'application/json',  
};

正确的是

var headersList = {
 'Accept': '*/*',
 'authorization': 'Bearer $token', // Authorization -> authorization
 'Content-Type': 'application/json',  
};

问题是否解决请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.