title: Spring @RequestBody working with Postman, but a 415 with javascript from browser
body: <p>I'm a starting web-developer and mostly done stuff with java on Spring boot. Just beginning to get familiar with javascript and right now having a problem that seems similar to many others here on stackoverflow, but the difference seems to be, that everything is working when POSTing with Postman, but when trying with browser via javascript, Il get a 415.</p>
  <p>The
  <code>alert(JSON.stringify(task))</code> gives (when &quot;taskName&quot;=&quot;walk&quot;): <code>{&quot;name&quot;:&quot;walk&quot;}</code></p>
  <p>When the same body is POSTed with Postman, the response is:</p>
  <pre><code>{
      &quot;id&quot;: 65,
      &quot;name&quot;: &quot;walk&quot;,
      &quot;new&quot;: false
  } 
  </code></pre>
  <p>Dependencies for jackson-databind, -core and  -annotations version 2.10.1 are in use.</p>
  <p>The javacode:</p>
  <pre><code>var url = contextRoot + &quot;tasks&quot;
  var http = new XMLHttpRequest()
  
  http.onreadystatechange = function() {
  
      alert(&quot;readystatechange&quot;)
  
      //if (this.readyState != 4) {
      //    return
      //}
         
      document.getElementById(&quot;received&quot;).innerHTML = this.responseText
      
  }
  
  function addTask() {
     
      var task = {
      name: document.getElementById(&quot;taskName&quot;).value
      }
  
      alert(JSON.stringify(task))
  
      http.open(&quot;POST&quot;, url)
      http.send(JSON.stringify(task))
  }
  </code></pre>
  <p>My @RestController</p>
  <pre><code>@RestController
  public class TaskController {
  
      @Autowired
      private TaskRepository taskRepository;
  
      @GetMapping(&quot;/tasks&quot;)
      public List&lt;Task&gt; list() {
          return taskRepository.findAll();
      }
  
      @PostMapping(path = &quot;/tasks&quot;, consumes = &quot;application/json&quot;, produces = &quot;application/json&quot;)
      public Task create(@RequestBody Task task) {
          System.out.println(&quot;!!!!!!!!!!!!!!!!!!!!!!!!!!&quot;);
          System.out.println(task);
          return taskRepository.save(task);
      }
      
      /*@PostMapping(&quot;/tasks&quot;)
      public String create(@RequestBody String task) {
          System.out.println(&quot;!!!!!!!!!!!!!!!!!!!!&quot;);
          System.out.println(task);
          return &quot;hilipataheijjaa&quot;;
      }*/
      
  }
  </code></pre>
  <p>HTML</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
      &lt;head lang=&quot;en&quot;&gt;
          &lt;meta charset=&quot;UTF-8&quot; /&gt;
          &lt;title&gt;Tasks&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;h2&gt;Tasks&lt;/h2&gt;
                  
          &lt;input type=&quot;text&quot; id=&quot;taskName&quot;/&gt;
          &lt;button onclick=&quot;addTask()&quot;&gt;Add Task&lt;/button&gt;
  
          
          &lt;p id=&quot;received&quot;&gt;
          &lt;/p&gt;
  
          &lt;script th:inline=&quot;javascript&quot;&gt; var contextRoot = /*[[@{/}]]*/ '';&lt;/script&gt;
          &lt;script th:src=&quot;@{/javascript/tasks.js}&quot; defer&gt;&lt;/script&gt;
          &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  <p>Task.java</p>
  <pre><code>import javax.persistence.Entity;
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.NoArgsConstructor;
  import org.springframework.data.jpa.domain.AbstractPersistable;
  
  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  @Entity
  public class Task extends AbstractPersistable&lt;Long&gt; {
  
      private String name;
  
  }
  </code></pre>
  <p>The XMLHttpRequest.responseText:</p>
  <blockquote>
  <p>{&quot;timestamp&quot;:&quot;2021-06-21T08:27:33.989+00:00&quot;,&quot;status&quot;:415,&quot;error&quot;:&quot;Unsupported
  Media
  Type&quot;,&quot;trace&quot;:&quot;org.springframework.web.HttpMediaTypeNotSupportedException:
  Content type 'text/plain;charset=UTF-8' not supported\r\n\tat
  org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:227)\r\n\tat
  org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:422)\r\n\tat
  org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)\r\n\tat
  org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:110)\r\n\tat
  org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:59)\r\n\tat
  org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:395)\r\n\tat
  org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1234)\r\n\tat
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1016)\r\n\tat
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\r\n\tat
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat
  org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\r\n\tat
  javax.servlet.http.HttpServlet.service(HttpServlet.java:652)\r\n\tat
  org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat
  javax.servlet.http.HttpServlet.service(HttpServlet.java:733)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
  org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
  org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
  org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat
  org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat
  org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat
  org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\r\n\tat
  org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat
  org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat
  org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)\r\n\tat
  org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat
  org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\r\n\tat
  org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)\r\n\tat
  org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat
  java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\r\n\tat
  java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\r\n\tat
  org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat
  java.base/java.lang.Thread.run(Thread.java:834)\r\n&quot;,&quot;message&quot;:&quot;Content
  type 'text/plain;charset=UTF-8' not supported&quot;,&quot;path&quot;:&quot;/tasks&quot;}</p>
  <blockquote>
  <p>Spring console:
  2021-06-21 11:27:02.189  WARN 5152 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
  [org.springframework.web.HttpMediaTypeNotSupportedException: Content
  type 'text/plain;charset=UTF-8' not supported] 2021-06-21 11:27:33.988
  WARN 5152 --- [nio-8080-exec-5]
  .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
  [org.springframework.web.HttpMediaTypeNotSupportedException: Content
  type 'text/plain;charset=UTF-8' not supported]</p>
  </blockquote>
  </blockquote>
  <p><strong>Thank You in advance!</strong></p>