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

Rah*_*hra 4 upload image http-headers dart flutter

我的API要求是

网址:/ user / upload-profile-image

方法=开机自检

标头-

Accesstoken:“ access_token”

内容类型=多部分/表单数据

这是我的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

Din*_*ian 8

你能尝试添加headers如下

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

final multipartRequest = new http.MultipartRequest('POST', Uri.parse(apiUrl))
multipartRequest.headers.addAll(headers);
multipartRequest.files.add(..)
Run Code Online (Sandbox Code Playgroud)


小智 5

  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);
Run Code Online (Sandbox Code Playgroud)