[英]how to get nested api data using nestjs httpservice (axios)
[英]Transfer request's file from API to API: NestJS(HttpService: Axios) to Python(flask)
我正在嘗試將文件從nestJS API 傳輸到 Python Flask ZDB974238714CA8DE634A7ACED0
此過程將由嵌套 API 上的 POST 請求(FormData:文件)觸發。 然后嵌套 api 應該將文件發送到 Python api。
NestJS 的 HttpService 使用 Axios。 所以我的目標基本上是從 NodeJS 發送帶有 axios 的文件。
FormData 在節點 JS 上不可用,所以我安裝了Nmp FormData 。
Python 代碼,我認為它工作正常,因為 Postman 請求通過沒有任何問題。
@app.route('/route', methods=['POST'])
def user():
params_data = json.load(request.files.get('file'))
return 'OK'
在鳥巢方面,我嘗試了很多東西。
但主要思想如下:使用 formData.getHeaders 作為 axios 標頭並將數據放入 axios 配置中。
@Post()
uploadFile(@Req() request: Request) {
// request is Express request
const formData: any = new FormData();
let newFile;
if (request.hasOwnProperty('file')) {
newFile = (request as any).file; // This is working
}
formData.append('file', newFile.buffer, 'filename');
return this.appService.launchOptim(formData);
}
public launchOptim(modelData: FormData) {
const axiosConfig: AxiosRequestConfig = {
headers: modelData.getHeaders(),
data: modelData,
};
return this.http.post('http://localhost:5000/route', modelData, axiosConfig)
.pipe(map(result => result.data));
}
然后,使用該配置,python 代碼中的request.files
始終為空。
如何使用 axios 將請求的文件正確傳輸到另一個 api?
@Post()
@UseInterceptors(FileInterceptor('file'))
uploadFile(@Req() request: Request, @UploadedFile() file,) {
var FormData = require("form-data");
const formData = new FormData();
formData.append('file', file.buffer, { filename: file.originalname });
const headers = {
...formData.getHeaders(),
"Content-Length": formData.getLengthSync()
};
await axios.post(requestAPI, formData, { headers });
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.