[英]I want to add header with access_token for flutter upload image
My API requirement is我的 API 要求是
URL: /user/upload-profile-image
method= POST
header--
Accesstoken: "access_token"
content-type = multipart/form-data
This is my code:这是我的代码:
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);
}
Can you try to add headers
like below您可以尝试添加如下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.