我使用 Web 服务进行图像处理,它在 Postman 中运行良好:
现在我想用 Dart 发送 http 请求:
import 'package:http/http.dart' as http;
static ocr(File image) async {
var url = '${API_URL}ocr';
var bytes = image.readAsBytesSync();
var response = await http.post(
url,
headers:{ "Content-Type":"multipart/form-data" } ,
body: { "lang":"fas" , "image":bytes},
encoding: Encoding.getByName("utf-8")
);
return response.body;
}
但是我不知道如何上传图片文件,在上面的代码我得到了例外: Bad state: Cannot set the body fields of a Request with content-type "multipart/form-data".
我应该如何编写请求的正文?