我想为颤振上传图像添加带有 access_token 的标头 [英] I want to add header with access_token for flutter upload image

查看:30
本文介绍了我想为颤振上传图像添加带有 access_token 的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can you try to add headers like below

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

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

这篇关于我想为颤振上传图像添加带有 access_token 的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆