dart 我如何在Flutter中添加授权和内容类型的头

2ic8powd  于 2024-01-03  发布在  Flutter
关注(0)|答案(1)|浏览(133)

我有这段代码来调用API;我如何向其中添加授权和内容类型头?

final url = Uri.parse('https://api.collectapi.com/economy/hisseSenedi');
  var counter;
  var hisseResult;
  Future callHisse() async {
    try{
      final response = await http.get(url);

      if(response.statusCode == 200){
        var result = hisselistFromJson(response.body);
        if(mounted);
        setState(() {
          counter = result.data.length;
          hisseResult = result;
        });
        return result;
      } else {
        print(response.statusCode);
      }
    } catch(e) {
      print(e.toString());
    }

  }

字符串

u3r8eeie

u3r8eeie1#

你可以这样做:

Map<String, String> requestHeaders = {
       'Content-type': 'application/json',
       'Authorization': '<Your token>'
     };

final response = await http.get(url,headers:requestHeaders);

字符串

相关问题