【发布时间】:2021-07-09 22:22:34
【问题描述】:
我正在尝试将 3 个图像文件发送到我在 Google Cloud Functions 上运行的快速服务器。
我能够让请求通过,但它在服务器上显示为缓冲区。
我正在设置需要在服务器上获取的请求字段,但不确定如何正确解析 req.body 以提取此信息。
Future<dynamic> uploadPost(Map<String, dynamic> postData, String placeName,
String placeAddress) async {
var request = http.MultipartRequest(
"POST",
Uri.parse('http://localhost:5001/findabite-1ac1a/us-central1/api' +
'/posts/add'));
request.fields['placeAddress'] = (placeAddress);
request.fields['placeName'] = (placeName);
request.headers['Content-Type'] = 'application/json';
await Future.forEach(postData.keys, (data) async {
final String key = data as String;
if (key.startsWith('filePath')) {
request.files.add(
await http.MultipartFile.fromPath(key, postData[key]),
);
} else {
request.fields[key] = (postData[key]);
}
});
request.send();
}
我的客户端实现中是否遗漏了什么?
这是我的快速配置:
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const { routes } = require("./src/routes");
const errorHandler = require("./src/middlewares/error_handler");
var app = express();
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
app.use(cors({ origin: true }));
app.disable("x-powered-by");
app.use(routes);
app.use(errorHandler);
module.exports = { app };
这是我尝试打印req.body时控制台的输出:
<Buffer 2d 2d 64 61 72 74 2d 68 74 74 70 2d 62 6f 75 6e 64 61 72 79 2d 34 68 59 48 5f 65 32 77 31 50 65 48 50 57 5f 31 31 4a 51 48 42 47 5a 39 46 4a 31 34 6b ... 309 more bytes>
【问题讨论】:
标签: node.js flutter express http