How to upload image on NestJS server from another service where image is in the form of URL?

Viewed 271

I have a controller made in my NestJS service S1, which accepts file using FileInterceptor, the function signature looks like this:

@UseInterceptors(FileInterceptor('file'))
@Post('uploads')
async uploadAttachment(@UploadedFile() file, @Query() queryParams: {filename: string}, @Res() res: Response, @Headers() header, @Req() req) 
{
//Some logic here
}

Now I want to use this API to upload an image from a different service S2, but I only have the image's publicly accessible URL

I am trying like this:


    import * as formData from 'form-data';
    import fetch from "node-fetch";

        let serv = await fetch(url);
        let img = await serv.buffer();
        let url = "http://localhost:8000/api/v1/ticketing/uploads?filename=checking.jpg"
        form.append("file", Readable.from(img));
    
    return firstValueFrom(this.httpService.post(url, form, {headers: {...header, ...form.getHeaders()}}).pipe(
        map((resp) => resp.data),
        catchError((_err) => {
          throw new HttpException(
            {success: false, error: {message: "failed"}, payload: _err.response.data},
            HttpStatus.BAD_REQUEST,
          );
        })
      )
    );
        

But this is not working, apparently the file is undefined on the controller of service S1. Could someone please help me out with this? I am new to NodeJS Also apologies for the poor editing.

0 Answers
Related