繁体   English   中英

我想为 flutter 上传图片添加带有 access_token 的标头

[英]I want to add header with access_token for flutter upload image

我的 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);

}

您可以尝试添加如下headers

Map<String, String> headers = { "Accesstoken": "access_token"};

final multipartRequest = new http.MultipartRequest('POST', Uri.parse(apiUrl))
multipartRequest.headers.addAll(headers);
multipartRequest.files.add(..)
  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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM