0

I have a problem configuring the header attribute in the @RequestMapping annotation.Here is my code: HTML page :

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <script type="text/javascript" th:src="@{/css/url_rearch_params.js}"/>
</head>
<body>
    <div id="app">
    <h1>TEST FORM</h1>
     <form action="" method="post">
        <p>Type description: <input type="text" v-model="type.description"/></p>
        <p><button v-on:click="addType()"> Send </button><input type="reset" value="Reset" /></p>
    </form>
    </div>


    <script src="http://cdn.jsdelivr.net/vue/1.0.10/vue.min.js"></script> 
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
      <script>
        Vue.prototype.$http = axios;
        new Vue({
            el:'#app',
            data:{
                type:{description:''}
            },
            methods:{
                addType(){
                    const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded',
                                                 'Accept': 'application/x-www-form-urlencoded'
                                              }
                                   };
                    let newType = {description:this.type.description};
                    console.log(newType);
                    this.$http.post('/types/insert',newType,config).then(response => {
                        console.log(response);
                    });
                }
            }
        });
      </script>
</body>
</html>

And my java code:

@RequestMapping(value = "/insert",method = RequestMethod.POST, headers = {"Accept=application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody void createType(@RequestBody Type type) {
    System.out.println(type);
    typeService.createType(type);
}

The problem if I try to execute the method I have the following message:

There was an unexpected error (type=Not Found, status=404).

If I remove :

headers = {“Accept=application/x-www-form-urlencoded”,“Content-Type = application/x-www-form-urlencoded”}

of the @requestpost parameter I have the following error :

There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported

N.B : I already visited this post but it does not solve my problem

Thank you in advance for your help.

kasko
  • 131
  • 1
  • 4
  • 14
  • 1
    I highly doubt that you are actually sending an `application/x-www-form-urlencoded`. Looks like a regular post with JSON and nothing url encoded. Also why are you messing around with headers yourself, you shouldn't need that. Remove the headers from the client (axios is smart enough to figure it out itself). – M. Deinum Dec 24 '17 at 10:15
  • Hi, actually, if I use consumes = "application / json" the method runs fine and I have the expected result back-end side.But in the browser I have this message : There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'application/x-www-form-urlencoded' not supported – kasko Dec 24 '17 at 18:55
  • Because that isn’t what you are sending, you are sending json back as well... – M. Deinum Dec 24 '17 at 19:03
  • And I do not know how to solve this problem.If you have an idea thank you for helping me. – kasko Dec 24 '17 at 19:05
  • As stated in my first comment remove the headers from your Javascript. Axios is smart enough to Figure it out. – M. Deinum Dec 24 '17 at 19:08
  • I deleted it but the problem persists. – kasko Dec 24 '17 at 19:21
  • Also make sure you don’t have anything url-encoded left on your controller. – M. Deinum Dec 24 '17 at 19:32
  • No I have not used url-encoded in the controller. – kasko Dec 24 '17 at 19:40
  • The only thing you need is ‘@PostMapping(/insert)’. No headers, consumes or produces on the controller and no headers on your JavaScript. You are overthinking the solution, keep it simple. – M. Deinum Dec 24 '17 at 20:20
  • In fact, it does not solve the problem. I still have the problem of : There was an unexpected error (type=Unsupported Media Type, status=415). Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported in the browser – kasko Dec 24 '17 at 20:36
  • Are you preventing the default form action? You might want to replace the form tags with a div as you aren’t really using it. If that still doesn’t work you are still configuring the content type somewhere... – M. Deinum Dec 24 '17 at 20:43
  • I tried the solution that you have proposed but it does not work.And I have not configured the content-type. – kasko Dec 25 '17 at 05:29
  • If you have that you still have headers set that send that content type. You should remove the `config` (at least the headers) from your `addType` function in javascript as well. – M. Deinum Dec 27 '17 at 08:21
  • Yes I did that but it did not work. I created a new topic : https://stackoverflow.com/questions/47990345/type-unsupported-media-type – kasko Dec 27 '17 at 10:21
  • Thanks, as you said I delete the form tag – kasko Jan 10 '18 at 06:00

1 Answers1

-1

Instead of headers, try consumes

@RequestMapping(value = "/insert",method = RequestMethod.POST, consumes="application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • Hi, in fact, if I use consumes = "application / json" the method runs fine and I have the expected result back-end side.But in the browser I have this message : There was an unexpected error (type=Unsupported Media Type, status=415).Content type 'application/x-www-form-urlencoded' not supported. – kasko Dec 24 '17 at 19:04
  • And I do not know how to solve this problem.If you have an idea thank you for helping me. – kasko Dec 24 '17 at 19:05
  • for that you need to set Accept and Content-Type headers to `application/x-www-form-urlencoded`. You cannot do this in browser. you can test it using some client like Postman. – pvpkiran Dec 25 '17 at 09:21