【问题标题】:I want to add header with access_token for flutter upload image我想为颤振上传图片添加带有 access_token 的标题
【发布时间】:2019-01-31 00:27:22
【问题描述】:

我的 API 要求是

URL: /user/upload-profile-image

method= POST

header--

Accesstoken: "access_token"

content-type = multipart/form-data

这是我的代码:

Future getUploadImg(File _image) async {

  String apiUrl = '$_apiUrl/user/upload-profile-image';

  final length = await _image.length();

  final request = new http.MultipartRequest('POST', Uri.parse(apiUrl))
      ..files.add(new http.MultipartFile('avatar', _image.openRead(), length));

  http.Response response = await http.Response.fromStream(await request.send());

  print("Result: ${response.body}");

  return JSON.decode(response.body);

}

【问题讨论】:

    标签: image upload dart http-headers flutter


    【解决方案1】:

    您可以尝试添加headers,如下所示

    Map<String, String> headers = { "Accesstoken": "access_token"};
    
    final multipartRequest = new http.MultipartRequest('POST', Uri.parse(apiUrl))
    multipartRequest.headers.addAll(headers);
    multipartRequest.files.add(..)
    

    【讨论】:

      【解决方案2】:
        var request = http.MultipartRequest(
          "POST",
          Uri.parse(
            "${Urls().url}/support/tenant/register",
          ),
        );
        //add text fields
        
        request.headers["authorization"]=userToken;
      
        request.fields["type"] = type;
        request.fields["note"] = note;
        for (var item in path) {
          var ext = item.split('.').last;
          var pic = await http.MultipartFile.fromPath("images", item, contentType: MediaType('image', ext));
          request.files.add(pic);
        }
      
        //add multipart to request
      
        var response = await request.send();
        var responseData = await response.stream.toBytes();
        var responseString = String.fromCharCodes(responseData);
      
        var d = jsonDecode(responseString);
      

      【讨论】:

        猜你喜欢
        • 2019-12-06
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 2021-04-22
        • 1970-01-01
        • 2021-11-09
        • 2019-10-06
        相关资源
        最近更新 更多