我知道这里有很多类似的问题,但是没人能解决我的问题...
我无法将服务器修改为支持base64 json格式,因此只能使用MultipartFile。
Future asyncFileUpload(File file) async {
var request = new http.MultipartRequest("POST", Uri.parse("url..."));
List<int> fileBytes = await file.readAsBytes();
var multipartFile = new http.MultipartFile.fromBytes('image',fileBytes);
request.files.add(multipartFile);
request.headers.addAll({'Authorization': 'Bearer eyJhbGciO...'},);
var response = await request.send();
if (response.statusCode == 200) print('Uploaded!');
}
但是在我的应用程序逻辑中,我在http.Client的对象上有一个InterceptorContract和RetryPolicy ...
import 'package:http/http.dart' as http;
http.Client client = HttpClientWithInterceptor.build(interceptors: [
HttpInterceptor(),
], retryPolicy: ExpiredTokenRetryPolicy());
class HttpInterceptor implements InterceptorContract {...}
class ExpiredTokenRetryPolicy extends RetryPolicy {...}
我确实以这种方式发布请求:
Future<bool> postNewProduct(ProductModel model) async {
var response = await client.post("url...", body: json.encode(model));
return response.statusCode == HttpStatus.ok;
}