title: Ajax POST FormData throw exception in Spring MVC
body: <p>Yesterday I had a similar problem where Ajax POST method returned 405 error which was caused by csrf token. Someone helped mi with this, but now I'm powerless what is happening. </p>
  
  <p>I have an Ajax POST request:</p>
  
  <pre><code>$(document).ready(function(){
      var mic, recorder, soundFile;
      setup();
  })
  
  
  function setup() {
    mic = new p5.AudioIn();
    mic.start();
    recorder = new p5.SoundRecorder();
    recorder.setInput(mic);
    soundFile = new p5.SoundFile();
  }
  
  function toggleRecording(e) {
      if (e.classList.contains("recording")) {
          recorder.stop();    
          e.classList.remove("recording"); 
          sendAudioToServer(soundFile)
      } else {
          e.classList.add("recording");
          recorder.record(soundFile);
      }
  }
  
  function sendAudioToServer(soundFile)
  {
      var data = new FormData();
      data.append('file', soundFile);
      $.ajax({
          method: 'POST',
          enctype: 'multipart/form-data',
          url: '/recognizeCommand',
          data: data,
          processData: false,
          contentType: false,
          success: function(data) {
            alert("works!");
          },    
          error: function(xhr, ajaxOptions, thrownError) {
              alert(xhr.status);
              alert(thrownError);
          }
      })
  }
  </code></pre>
  
  <p>soundFile is an object from p5.js library which contain audio. I also try with simple String but there is the same error</p>
  
  <p>And a controller in Spring MVC:</p>
  
  <pre><code>@RequestMapping(value = "/recognizeCommand", method = RequestMethod.POST)
      public @ResponseBody String recognizeCommand(@RequestParam("file") MultipartFile multipartFile) {
          try {
              SpeechRecognitionApplication.logger.info("BEFORE: " + multipartFile);
              byte[] bytes = multipartFile.getBytes();
              SpeechRecognitionApplication.logger.info(bytes);
          } catch (IOException e) {
              e.printStackTrace();
          }
          return "finish";
      }
  </code></pre>
  
  <p>When I send this Ajax request it throws error 400 and there is exception in Spring:</p>
  
  <pre><code>org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
      at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:199) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:112) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) [spring-webmvc-5.0.5.RELEASE.jar:5.0.5.RELEASE]
  </code></pre>
  
  <p>What is important that when I tested endpoint with postman and send some file as parameter it works correctly.</p>
  
  <p>I know there are similar posts on this forum but seriously I checked each of them, try every solution and nothing can help.
  I believe that some of you will have any idea how to solve this problem.</p>
  
  <p><strong>EDIT</strong>
  Added this 3 lines before ajax post method:</p>
  
  <pre><code>data.append('file', "example");
  console.log("file: " + data.get("file"));
  console.log(data);
  </code></pre>
  
  <p>returns:</p>
  
  <p><a href="https://i.stack.imgur.com/xA3d1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xA3d1.png" alt="enter image description here"></a></p>