Forwarding file uploads between NestJS and Flask APIs using Axios

I need help with passing files between two different APIs. I have a NestJS backend that receives file uploads and needs to forward them to a Flask API.

Here’s what happens: A user uploads a file to my NestJS endpoint, then I want to send that same file to my Python Flask server.

My Flask endpoint works fine when I test it directly:

@app.route('/process', methods=['POST'])
def handle_upload():
    file_content = json.load(request.files.get('upload'))
    return 'Success'

But I’m having trouble with the NestJS side. I’m using the form-data package since FormData isn’t built into Node:

@Post('upload')
processFile(@Req() req: Request) {
    const formData: any = new FormData();
    let uploadedFile;
    if (req.hasOwnProperty('upload')) {
        uploadedFile = (req as any).upload;
    }
    formData.append('upload', uploadedFile.buffer, 'data.json');
    return this.fileService.sendToFlask(formData);
}

And in my service:

public sendToFlask(fileData: FormData) {
    const config: AxiosRequestConfig = {
        headers: fileData.getHeaders(),
        data: fileData,
    };
    
    return this.httpService.post('http://localhost:5000/process', fileData, config)
        .pipe(map(response => response.data));
}

The problem is that request.files is always empty on the Flask side. What’s the right way to configure Axios for file transfers?

Had the same setup - Express forwarding files to a Python service. Your problem’s probably how you’re building the FormData in NestJS. Don’t use req.upload to grab the file. Extract the file data from the request first, then append the actual stream or buffer to FormData. Also, don’t set conflicting headers - let FormData handle the boundary stuff automatically. I had to read the incoming file as a stream and pipe it to FormData before sending through Axios. Flask should get the file fine if the multipart boundaries are right.

I hit this exact same issue building a microservice last year. Your Axios config is the problem - you’re passing the config object wrong and duplicating the data parameter.

Here’s what fixed it for me:

public sendToFlask(fileData: FormData) {
    const config: AxiosRequestConfig = {
        headers: {
            ...fileData.getHeaders(),
            'Content-Type': 'multipart/form-data'
        }
    };
    
    return this.httpService.post('http://localhost:5000/process', fileData, config)
        .pipe(map(response => response.data));
}

Two key fixes: drop the data property from config since you’re already passing fileData as the second parameter, and explicitly set the Content-Type header. Also check that you’re handling the file buffer properly in your NestJS controller - you’ll probably need multer middleware to parse the multipart data before creating the FormData object.

check your nestjs controller setup first - you’re missing the multer middleware for file uploads. without it, req.upload won’t exist. also, try using axios directly instead of httpService. the nestjs wrapper sometimes messes up formdata headers.