将请求的文件从API传输到API:NestJS(HttpService:Axios)到Python(flask) [英] Transfer request's file from API to API: NestJS(HttpService: Axios) to Python(flask)

查看:214
本文介绍了将请求的文件从API传输到API:NestJS(HttpService:Axios)到Python(flask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从nestJS API传输到Python Flask API.

此过程将由Nest API上的POST请求(FormData:文件)触发.然后,嵌套api应该将文件发送到Python api.

nestJS的HttpService使用Axios.因此,我的目标基本上是从NodeJS使用axios发送文件.

FormData在节点JS上不可用,因此我安装了 Nmp FormData .

Python

Python代码,我认为它可以正常运行,因为Postman请求通过了,没有任何问题.

 <代码> @ app.route('/route',methods = ['POST'])def user():params_data = json.load(request.files.get('file'))返回确定" 

NestJS

在嵌套方面,我做了很多尝试.

但是主要思想是:使用formData.getHeaders作为axios标头,并将数据放入axios配置中.

app.controller.ts

 <代码> @Post()uploadFile(@Req()请求:请求){//请求是Express请求const formData:any = new FormData();让newFile;如果(request.hasOwnProperty('file')){newFile =(任何要求).file;//这正在工作}formData.append('file',newFile.buffer,'filename');返回this.appService.launchOptim(formData);} 

app.service.ts

 公共launchOptim(modelData:FormData){const axiosConfig:AxiosRequestConfig = {标头:modelData.getHeaders(),数据:modelData,};返回this.http.post('http://localhost:5000/route',modelData,axiosConfig).pipe(map(result => result.data));} 

然后,使用该配置,python代码中的 request.files 始终保持为空.

如何使用axios将请求的文件正确传输到另一个api?

有关此主题的问题: Axios问题还尝试了这个 Axios修复

解决方案

  @Post()@UseInterceptors(FileInterceptor('file'))uploadFile(@Req()请求:请求,@ UploadedFile()文件,){var FormData = require("form-data");const formData = new FormData();formData.append('file',file.buffer,{文件名:file.originalname});const标头= {... formData.getHeaders(),内容长度":formData.getLengthSync()};等待axios.post(requestAPI,formData,{标头});} 

I am trying transfer a file from a nestJS API to Python Flask API.

This process will be triggered by a POST request (FormData: file) on nest API. Then the nest api should send the file to Python api.

The HttpService from nestJS use Axios. So my goal is basically to send file with axios from NodeJS.

FormData is not available on node JS so I installed Nmp FormData .

Python

Python Code, which I think is working properly because Postman request pass without any problems.

@app.route('/route', methods=['POST'])
def user():

    params_data = json.load(request.files.get('file'))

    return 'OK'

NestJS

On the nest side, I tried a lot a things.

But the main idea is the following: Use formData.getHeaders as axios headers and put data inside the axios config.

app.controller.ts

    @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);
    }

app.service.ts

  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));

  }

And then, with that configuration, request.files in python code stays always empty.

How to correctly transfer request's file to another api with axios ?

Issue about this topic: Axios issue Also tried this Axios fix

解决方案

@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 });
}

这篇关于将请求的文件从API传输到API:NestJS(HttpService:Axios)到Python(flask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆