【问题标题】:How to pass Authorization header in API POST call in dart?如何在飞镖的 API POST 调用中传递授权标头?
【发布时间】:2021-04-02 02:31:29
【问题描述】:

我正在使用颤振应用程序,我想知道我们如何在 POST 请求中传递授权标头。这是我尝试过的代码。我需要帮助才能知道如何以正确的方式做到这一点。

    Future<String> addCartItemToDb({String userId}) async {
    try {
      final res = await http.post(
        _baseURL,
        headers: {
          "Content-Type": "application/json",
          "accept": "*/*",
          "Authorization": "token"
        },
        body: jsonEncode({"userId": userID})
      );
      return _response(res);
    }on SocketException {
      throw CustomException("No internet Connection");
    }on FormatException {
      throw CustomException("Bad Response format !");
    }
  }

此外,我想知道我们如何解码 token 我在标头中传递的 userid 给那个已经登录的用户。

【问题讨论】:

标签: api flutter authentication dart post


【解决方案1】:
final uri = Strings.base_url_api+Strings.nearby_cons;

var requestBody = {
  "organization_id": _orgID.toString()
};

http.Response response = await http.post(uri,
  headers: <String, String>{
    'Content-Type': 'application/json',
    'Authorization': 'Bearer $_auth'
  },
  body: requestBody,
);

if (response.statusCode == 200) {

  var responseJson = json.decode(response.body);
  print(responseJson);

} else {
  print('Status code error.. Check your API method or URL.. ');
}

【讨论】:

    【解决方案2】:

    final jobsListAPIUrl = constants.BASE_URL+'bookings/retail/0/20/?status='+constants.BOOKING_CONFIRM_KEY+'&filter=0'; 打印(作业列表APIUrl); var response = await http.get(jobsListAPIUrl,headers: {'Authorization':'Token'+widget.token}); 打印(response.body);

    【讨论】:

      猜你喜欢
      • 2019-11-06
      • 2020-03-09
      • 1970-01-01
      • 2020-04-10
      • 2018-05-25
      • 2012-02-26
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多