I am trying to send a file to api in a next js app. The image will be uploaded to cloudinary:
Function calling api is:
async uploadButtonClicked(imageUpload) {
const formData = new FormData();
//formData.append('test', "testing");
formData.append('postPic', imageUpload.files[0]);
const res = await fetch('/api/hello', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
},
body: formData,
})
console.log(imageUpload.files[0])
in front end gives me the values below and looks good.
In the api,
export default (upload.single('postPic'), async (req, res) => {
console.log(req.body)
The above is undefined
when I use
export const config = {
api: {
bodyParser: false,
},
};
When I remove the bodyParser setting (bodyParser is true), the data is as a stream which is not useful for upload. I get a upload error as shown below:
Cloudinary will upload if the body reaches the api in this format below:
What should be changed to get the body (which is basically an image file) to reach the api in the proper format as shown above?
I think the question can also be asked as: Why is the req.body undefined
when using bodyParser: false
?