express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

question: file upload using axios and validator

Open tamis-laan opened this issue 3 years ago • 0 comments

Using the following route specification:

 /user/avatar:
   patch:
     operationId: patchUserAvatar
     summary: Upload user avatar
     description: Upload a user avatar
     tags:
       - Users
     requestBody:
       description: Avatar image
       required: true
       content:
         multipart/form-data:
           schema:
             type: object
             properties:
               avatar:
                 type: string
                 format: binary
                 description: Avatar image
             required:
               - avatar

I'm trying to upload an image from the frontend using axios as followed:

const payload = new FormData()
payload.append('avatar', image)
axios.patch(`/user/avatar`, payload, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})

Where image has the structure:

{
    name: ..., 
    type: ...,
    uri: ....,
}

and represents a file on disk.

However express-openapi-validator returns the following error:

{
    "errors": [{
        "message":"multipart file(s) required","path":"/user/avatar"
    }],
    "name":"Bad Request",
    "status":400
}

Given the error it's not entirely clear what is going wrong. The header is set to multipart and the form attaches one file. Can anyone see what is going wrong here?

tamis-laan avatar Feb 09 '22 12:02 tamis-laan