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

问题描述 投票:2回答:1

我的API要求是

网址:/ 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个回答
2
投票

你可以试着像下面这样添加headers

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

final multipartRequest = new http.MultipartRequest('POST', Uri.parse(apiUrl))
multipartRequest.headers.addAll(headers);
multipartRequest.files.add(..)
© www.soinside.com 2019 - 2024. All rights reserved.