I have created a very simple controller for a REST services using the Spring Framework. I have no problems using the @PathVariable in conjunction with GETs but when I try to create a POST or PUT, I am unable to curl the function and am trying to figure out where I have gone wrong.
@Controller
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public String putName(@RequestParam("name") String name, @RequestParam("value") String value) {
System.out.println("Name - " + name);
System.out.println("Value - " + value);
return "You entered " + name + " with value of " + value;
}
}
Editing this a bit to include some PHP since that is ultimately where I want to be calling this from.
If I do the following, it works as expected:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/RestService/test');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'name=some random name&value=just a value');
$result = curl_exec($ch);
But if I try to pass the parameters as an array, I receive an HTTP Status 400 -The request sent by the client was syntactically incorrect.
$data = array
(
'name' => 'some random name',
'value' => 'just a value'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/RestService/test');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result2 = curl_exec($ch);
If I can at all help it, I do not want to be building strings, worrying about escaping ampersands, and etc out of the data I want to pass.
Any help is greatly appreciated.