Reputation: 37
I am trying to send a simple String from a HTML form to my REST Web service based on spring, but even after numerous attempts I am unable to send the data to my webservice, every time I get "The request sent by the client was syntactically incorrect" (400) error. Can someone please look into the code snippet and let me know what I am missing out?
<form action="http://localhost:8080/SpringRest/rest/store/prolist" method="GET">
<p>
Products : <input type="text" name="listofproducts" id = "listofproducts"/>
</p>
<input type="submit" value="Get the Lowest Price" />
</form>
Code in Controller:
@RequestMapping(value = "http://localhost:8080/SpringRest/rest/store/prolist/{listofproducts}", method = RequestMethod.GET)
public @ResponseBody String getListOfProducts(@PathVariable String listofproducts) {
return listofproducts;
}
Upvotes: 2
Views: 4833
Reputation: 3935
When using GET to send form data, you'll have to receive it as a request parameter not path variable because the form data is sent to the server as a request param in following format
http://host_port_and_url?name1=value1&name2=value2&more_params
So do this instead
@RequestMapping(value = "http://localhost:8080/SpringRest/rest/store/prolist", method = RequestMethod.GET)
public @ResponseBody String getListOfProducts(@RequestParam(value = "listofproducts") String listofproducts) {
return listofproducts;
}
You can check this out
Notes on GET:
Appends form-data into the URL in name/value pairs The length of a URL is limited (about 3000 characters) Never use GET to send sensitive data! (will be visible in the URL) Useful for form submissions where a user want to bookmark the result GET is better for non-secure data, like query strings in Google
And also you don't need absolute URI mapping with host, port etc. Just use controller mapping. Assuming store
is controller context path, this will suffice
@RequestMapping(value = "store/prolist", method = RequestMethod.GET)
Upvotes: 3