【问题标题】:Flutter + DIO: How to pass options from curl command in a request via DIO?Flutter + DIO:如何通过 DIO 在请求中传递 curl 命令的选项?
【发布时间】:2021-06-29 23:03:26
【问题描述】:

我正在尝试向 NextCloud 服务器执行 GET 请求。 NextCloud 文档给出了如何使用 curl 命令执行请求的示例:

curl -u 用户名:密码 -X GET 'https://cloud.example.com/ocs/v1.php/...' -H "OCS-APIRequest: true"

如何在 dio 中添加 -u username:password 之类的选项? 我已经阅读了 DIO 文档上下,但我无法找出解决方案...

提前非常感谢!

【问题讨论】:

    标签: flutter dart httprequest nextcloud dio


    【解决方案1】:

    试试这个:) 或者对于 Dio,只使用下面的标题

    import 'dart:convert';
    import 'package:http/http.dart' as http;
    
    void main() async {
      var uname = 'username';
      var pword = 'password';
      var authn = 'Basic ' + base64Encode(utf8.encode('$uname:$pword'));
    
      var headers = {
        'OCS-APIRequest': 'true',
        'Authorization': authn,
      };
    
      var res = await http.get('https://cloud.example.com/ocs/v1.php/', headers: headers);
      if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
      print(res.body);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2020-04-12
      • 2021-08-03
      • 2021-09-29
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多