2

I'm trying to send json with image to another api from my nesjs api, I'm using httpservice.post but always getting an error - "no file supplied".

the image is ulpoaded from the user and saved in the public folder, then I try to get the image from the folder and send it to the api.

in the remote api they expect - raw data image and Content-Type: multipart/form-data in the header, I tried both but it doesn't work.

this is the code:

const image = await fs.createReadStream(`./public/storage/images/${image.filename}`);

let imageData = {
            "file": image,
            "caption": tag,
            "isLogo": false
}

const res = await this.httpService.post(
                    `${urlapi}`,
                   imageData,
                    {   
                        headers: {
                            "Authorization": `Bearer ${access_token}`,
                            "Content-Type": "application/json"
                        }   
                    }
                )
                .toPromise();

I'm new at nestjs and node, I would really appreciate some help.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • You can read the image that you want to send as a raw and send it via your API – Rebai Ahmed Sep 02 '20 at 09:05
  • how do i do that? everything i tried doesn't work. – user14207225 Sep 02 '20 at 09:22
  • Does the image in question exist at `/dist/public/storage/images/${image.filename}`? If not, that would explain why there's no file there. – Jay McDoniel Sep 02 '20 at 16:30
  • yes, i was able to copy it to another folder, i think my problem is with sending the image in the httpservie. i also tried to use axios but it doesn't work. – user14207225 Sep 03 '20 at 18:34
  • NestJS is using axios, so you can google for that to get more results. I think this could solve your task: https://stackoverflow.com/a/53040878/6533425 - Basically you need a new FormData object and attach the image read stream to it. That you pass to axios. – CaKa Sep 04 '20 at 16:57
  • i tried it with form data and axios, still the same error - "no file supplied" :( – user14207225 Sep 07 '20 at 07:48

0 Answers0