我想添加带有access_token的标题以用于抖动上传图片

时间:2018-08-24 13:48:05

标签: image upload dart http-headers flutter

我的API要求是

URL:/ user / upload-profile-image

method = POST

标题-

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

}

2 个答案:

答案 0 :(得分: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(..)

答案 1 :(得分:0)

Photobook