I'm currently making a simple function that creates a Form Data, append necessary variables then send it to an external server. This server receives variables listed in form.append and one of those variables requires an image file to be sent. Here's my code:
router.get('/test-poster',upload.any(), async (req, res) => {
const form = new FormData();
form.append("file", fs.createReadStream('../../backendsmartattendance/backend_server/uploads/tmp/image.jpg'), {
contentType: 'image/jpeg',
filename: 'image.jpg',
});
form.append("phone", '085273396452');
form.append("message", 'This is a test message');
form.append("token", 'LQanvrN2EkDp3mQWy')
axios.post(`https://api.sparks.id/api/whatsapp/image`, form, {
headers: {
'Content-Type': 'application/json'
}
})
.then(()=>{
return res.status(200).json({code:200,status:true,message:'Data successfully sent'});
})
.catch((error)=>{
return res.status(500).json({code:500,status:false,message:error});
})
})
When I run it, I get an error "Request failed with status code 415" which I assume is because the server does not accept my format, but I'm not sure of why exactly the server rejects it. I firstly thought I didn't send the image file, so I tried to send a request without the 'file' field in the formData, this still results in the same error code. Then I thought the server maybe only accepts an object instead of a formData (which was specifically said in documentation it only accepts formData) and of course it still results in the same error code. I have no idea what causes this problem and I'm pretty new on how to properly send node http requests, so any help will be highly appreciated.
===
Update
Thank your for the people that helped giving me inputs, its really helpful and my code has worked on sending the file but it seems like the format is not what the server expect. I've read the server's documentation and it give an example in curl like this to send a file:
curl -X 'POST' 'https://api.sparks.id/api/whatsapp/image' -F 'file=@245907736_1159912484416576_7427838087540430398_n.jpg;type=image/jpeg'
And so I tried to send the file in a formData like this:
const form = new FormData();
form.append("file", fs.readFileSync('../../backendsmartattendance/backend_server/uploads/tmp/image.jpg'), {
contentType: 'image/jpg',
filename: 'image.jpg',
});
axios.post(`https://api.sparks.id/api/whatsapp/image`, data, {
headers:{
"Accept": 'application/json',
"Content-Type":"multipart/form-data"
}
})
.then((resp)=>{
return res.status(200).json({code:200,status:true,message:'Data successfully sent'});
})
.catch((error)=>{
return res.status(500).json({code:500,status:false,message:error});
})
But it doesn't work (still returns error code 422: The given data is invalid/wrong format), I'm quite confused because isn't this how you're supposed to send the image in the formData? Or have I convert the curl syntax wrong?