yonatan hornstein
yonatan hornstein

Reputation: 233

Axios POST request: Content-type is set, but inside spring it's empty

I'm sending video as POST request using Axios with 'Content-Type': 'multipart/form-data', but inside Spring I see the following error:

"[org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]" 

The Axios code :


async function uploadPost() {
    const type = 'video/mp4';

    let data = new FormData();
    const videoUri = imagePreview;

    data =
      ('video',
      {
        name: 'mobile-video-upload',
        type,
        videoUri,
      });
    axios({
      method: 'post',
      url: 'http://localhost:8080/post/upload',
      body: data,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }).catch(function(error) {
      console.log(error);
    });


The function inside Spring :

   @CrossOrigin
   @ResponseBody
   @RequestMapping(value = "/post/upload",  method = RequestMethod.POST, consumes = "multipart/form-data")
   public String create(@RequestParam("file") MultipartFile file) {

Thanks in advance.

Upvotes: 0

Views: 560

Answers (1)

PythonLearner
PythonLearner

Reputation: 1466

Assuming Spring code to be correct, please try with minor changes with Axios.

const axios = require('axios')
const qs = require('querystring')
....... other code .....

const config = {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
}

axios.post(url, qs.stringify(requestBody), config)
  .then((result) => {
    // Do somthing
  })
  .catch((err) => {
    // Do somthing
  })

Upvotes: 1

Related Questions