url routing - Grails 'method not allowed' weirdness -
i have simple controller code like:
// usercontroller.groovy class usercontroller { static allowedmethods = [ signin: 'get', authenticate: 'post', signout: 'post', register: 'get', save: 'post' ] // ... code omitted def register() { } def save() { render 'ok' } } registration form:
<!-- register.gsp --> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="layout" content="main" /> <title>withme: register</title> </head> <body> <g:form mapping="register"> <!-- code omitted --> <g:actionsubmit value="register" /> </g:form> </body> </html> and url mappings:
//urlmappings.groovy class urlmappings { static mappings = { name register: '/register'(controller: 'user') { action = [get: 'register', post: 'save'] } } } now weird part. when making request curl works expected:
$ curl http://localhost:8080/withme/register -x -v -l ... form rendered ... $ curl http://localhost:8080/withme/register -x post -v -l < http/1.1 200 ok < server: apache-coyote/1.1 < content-type: text/html;charset=utf-8 < transfer-encoding: chunked < date: thu, 08 aug 2013 07:41:23 gmt < * connection #0 host localhost left intact ok* closing connection #0 accessing /register browser returns form. when submit within browser, 405 returned!
http status 405 - type status report message description specified http method not allowed requested resource. apache tomcat/7.0.42 firebug confirms post performed:
post http://localhost:8080/withme/register | 405 method not allowed | localhost:8080 i have disabled filters in application.
i wonder difference between curl , browser post requests? why grails handles in different way? , finally, how fix it..
grails 2.2.4 groovy version: 2.1.6 jvm: 1.7.0_21 vendor: oracle corporation os: mac os x
update
i've figured out request body matters. sending request curl without body succeed, setting body causes request fail. one:
$ curl http://localhost:8080/withme/register -x post -v -l * connect() localhost port 8080 (#0) * trying ::1... * connected * connected localhost (::1) port 8080 (#0) > post /withme/register http/1.1 > user-agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8x zlib/1.2.5 > host: localhost:8080 > accept: */* > < http/1.1 200 ok < server: apache-coyote/1.1 < content-type: text/html;charset=utf-8 < transfer-encoding: chunked < date: thu, 08 aug 2013 09:39:22 gmt < * connection #0 host localhost left intact ok* closing connection #0 and bad one:
$ curl http://localhost:8080/withme/register -x post -v -l -d "email=&password=&passwordconfirmation=&firstname=&lastname=&country=&city=&_action_register=register" * connect() localhost port 8080 (#0) * trying ::1... * connected * connected localhost (::1) port 8080 (#0) > post /withme/register http/1.1 > user-agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8x zlib/1.2.5 > host: localhost:8080 > accept: */* > content-length: 100 > content-type: application/x-www-form-urlencoded > * upload sent off: 100 out of 100 bytes < http/1.1 405 method not allowed < server: apache-coyote/1.1 < content-type: text/html;charset=utf-8 < content-length: 977 < date: thu, 08 aug 2013 09:40:31 gmt < * connection #0 host localhost left intact <html><head><title>apache tomcat/7.0.42 - error report</title><style><!--h1 {font-family:tahoma,arial,sans-serif;color:white;background-color:#525d76;font-size:22px;} h2 {font-family:tahoma,arial,sans-serif;color:white;background-color:#525d76;font-size:16px;} h3 {font-family:tahoma,arial,sans-serif;color:white;background-color:#525d76;font-size:14px;} body {font-family:tahoma,arial,sans-serif;color:black;background-color:white;} b {font-family:tahoma,arial,sans-serif;color:white;background-color:#525d76;} p {font-family:tahoma,arial,sans-serif;background:white;color:black;font-size:12px;}a {color : black;}a.name {color : black;}hr {color : #525d76;}--></style> </head><body><h1>http status 405 - </h1><hr size="1" noshade="noshade"><p><b>type</b> status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>the specified http method not allowed requested resource.</u></p><hr size="1" noshade="noshade"><h3>apache tomcat/7.0.42</h3></body></html>* closing connection #0 still what's difference?
i've solved strange problem.
grails renders <g:actionsubmit value="register" />
<input type="submit" value="register" name="_action_register"> note strange name. grails deduce value attribute , uses when choosing action call. causes problem in case because post /register should handled save action.
changing <input type="submit" value="register"> solve problem.
Comments
Post a Comment