RestfulController not behaving as expected - SOLVED!

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

RestfulController not behaving as expected - SOLVED!

jondo_w
This post was updated on .
I'm trying to setup a RestfulController based backend to an Ember app.

Some specifics:
Grails 2.3.4
Ember + Ember-RESTless

I've tried to follow the documentation in http://grails.org/doc/latest/guide/webServices.html#restfulControllers as best as I know how, but for some reason when doing a POST to create a new record, I'm not getting the default behaviour working as expected.

My code is as follows:

Structure of JSON submitted in AJAX request:

{"serviceProviderType":{"name":"Assessor","image":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACuElEQVR42o2Ta0jTURjGB5JupUWN0EBIZkzn1OWGy+FMN3dxuSGmg1VTl5o2rbxsS526rc1LJs68MfNDlhB+LqzUIvP2n85JIFZmRRHhBz8EgalB9bQJGSGmLzycw+E8v3Peh3NIpB1KqbT4HpaV5pJTqlfI8tp1X2GJkrSrkuj3/ZnS89pBrSRArXKCoTL+2t7E1x6kSA33/fP64SerWibFFTG8y9ICM6gV40huc0OUmYNt/T4CXefe8/0I0E3Av+QJKJkt2JPWhEDDU3CbppF8Rgu+JPU7k8c7FKy0U7YABLk1nwLze5HWOg5esxuhZgJHawkwbE6ktM8gNrMIx5U6sFTVSLjiwBFJccM/gIwLBpNKU+iWZaih7JpAhJUAq94JdsM06NcIhNfNIszmAs1EIMTqBk2U/WPDSBaV0SgZLTP7z3aCKq9CaKoWDPMoYhqnQDdPIsQ04QES4ORYILnUDOH1ZxDedIObKPHmofQhp1S8D9CNgXp1zHNlJ6Lq3Yism0a4dRI0sxMF9xbAFysgUiiRJEsHT5ACllQNDl88TPJLLFT4q3sQZJwA3TIJTuM0+PZZyLpfQux4jRDbHMQ3XaBHsH7SY7isSBZXHs05oWewYq3hkWw56YCo5GGwftBz4hSiPf2Ge/rO7nqO+HPlSKp7jAj7W0S1fcCxmlEEZnesB5yqnvFTNJRuBsc8Xb4m73BB1jWHCBsBieMV2KkaxCUKkWwdAKd7CdyeJXAcn8Fs+wj5rXmwE6SjmwBDZc16zmUjChvvQG0fANsTFDOajbB03Yiq7x0Mw19hHFmBfugLih8so/XRPHRG84u/b12jCcovrSjO0ur60rMu4qRUgdDQsLay5tvV8QIpdtJ/v4N3g6V3cEPWu0MweUavFhbeYHFxcXeAuBuuLfq2uoq1tbXdAXbSbyFNjWsoy4bmAAAAAElFTkSuQmCC"}}

My mapping in UrlMappings is as follows:

        "/service_provider_types"(resources: "serviceProviderType", parseRequest: true)

(I've tried with parseRequest:true and without it specified, same result)


My controller class is as follows:
class ServiceProviderTypeController extends RestfulController<ServiceProviderType> {

    static responseFormats = ['json', 'xml'];

    ServiceProviderTypeController() {
        super(ServiceProviderType);
    }

}


My ServiceProviderType class is as follows:
class ServiceProviderType {

    String name;
    byte[] image;

    static constraints = {
        name(nullable: false)
        image(nullable: true)
    }
}

Now aside from the fact that my Ajax call sends a String image and the domain class field is a byte[], the issue I'm having is that neither name nor image is coming through in the request params. If I set a breakpoint in RestfulController at this line, it hits the line as expected, but the params are not as expected:

    @Transactional
    def save() {
        def instance = createResource(getParametersToBind())

        ...
    }

The getParametersToBind() has the action: 'delete', and the controller: 'serviceProviderType' and the RestfulController instance is of the correct subclass, so my mapping is working.

But everything fails because the params do not contain the expected content. I *think* I'm doing everything correctly, so what am I not doing correctly. Any ideas appreciated. :-(

Thanks,
Darryl
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected

sergiomichels
I've created a similar issue, a while ago: http://jira.grails.org/browse/GRAILS-10431

--
Sérgio Michels


On Fri, Jan 17, 2014 at 7:02 AM, jondo_w <[hidden email]> wrote:
I'm trying to setup a RestfulController based backend to an Ember app.

Some specifics:
Grails 2.3.4
Ember + Ember-RESTless

I've tried to follow the documentation in
http://grails.org/doc/latest/guide/webServices.html#restfulControllers as
best as I know how, but for some reason when doing a POST to create a new
record, I'm not getting the default behaviour working as expected.

My code is as follows:

Structure of JSON submitted in AJAX request:

{"serviceProviderType":{"name":"Assessor","image":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACuElEQVR42o2Ta0jTURjGB5JupUWN0EBIZkzn1OWGy+FMN3dxuSGmg1VTl5o2rbxsS526rc1LJs68MfNDlhB+LqzUIvP2n85JIFZmRRHhBz8EgalB9bQJGSGmLzycw+E8v3Peh3NIpB1KqbT4HpaV5pJTqlfI8tp1X2GJkrSrkuj3/ZnS89pBrSRArXKCoTL+2t7E1x6kSA33/fP64SerWibFFTG8y9ICM6gV40huc0OUmYNt/T4CXefe8/0I0E3Av+QJKJkt2JPWhEDDU3CbppF8Rgu+JPU7k8c7FKy0U7YABLk1nwLze5HWOg5esxuhZgJHawkwbE6ktM8gNrMIx5U6sFTVSLjiwBFJccM/gIwLBpNKU+iWZaih7JpAhJUAq94JdsM06NcIhNfNIszmAs1EIMTqBk2U/WPDSBaV0SgZLTP7z3aCKq9CaKoWDPMoYhqnQDdPIsQ04QES4ORYILnUDOH1ZxDedIObKPHmofQhp1S8D9CNgXp1zHNlJ6Lq3Yism0a4dRI0sxMF9xbAFysgUiiRJEsHT5ACllQNDl88TPJLLFT4q3sQZJwA3TIJTuM0+PZZyLpfQux4jRDbHMQ3XaBHsH7SY7isSBZXHs05oWewYq3hkWw56YCo5GGwftBz4hSiPf2Ge/rO7nqO+HPlSKp7jAj7W0S1fcCxmlEEZnesB5yqnvFTNJRuBsc8Xb4m73BB1jWHCBsBieMV2KkaxCUKkWwdAKd7CdyeJXAcn8Fs+wj5rXmwE6SjmwBDZc16zmUjChvvQG0fANsTFDOajbB03Yiq7x0Mw19hHFmBfugLih8so/XRPHRG84u/b12jCcovrSjO0ur60rMu4qRUgdDQsLay5tvV8QIpdtJ/v4N3g6V3cEPWu0MweUavFhbeYHFxcXeAuBuuLfq2uoq1tbXdAXbSbyFNjWsoy4bmAAAAAElFTkSuQmCC"}}

My mapping in UrlMappings is as follows:

        "/service_provider_types"(resources: "serviceProviderType",
parseRequest: true)

(I've tried with parseRequest:true and without it specified, same result)


My controller class is as follows:
class ServiceProviderTypeController extends
RestfulController<ServiceProviderType> {

    static responseFormats = ['json', 'xml'];

    ServiceProviderTypeController() {
        super(ServiceProviderType);
    }

}


My ServiceProviderType class is as follows:
class ServiceProviderType {

    String name;
    byte[] image;

    static constraints = {
        name(nullable: false)
        image(nullable: true)
    }
}

Now aside from the fact that my Ajax call sends a String image and the
domain class field is a byte[], the issue I'm having is that neither name
nor image is coming through in the request params. If I set a breakpoint in
RestfulController at this line, it hits the line as expected, but the params
are not as expected:

    @Transactional
    def save() {
        def instance = createResource(getParametersToBind())

        ...
    }

The getParametersToBind() has the action: 'delete', and the controller:
'serviceProviderType' and the RestfulController instance is of the correct
subclass, so my mapping is working.

But everything fails because the params do not contain the expected content.
I *think* I'm doing everything correctly, so what am I not doing correctly.
Any ideas appreciated. :-(

Thanks,
Darryl



--
View this message in context: http://grails.1312388.n4.nabble.com/RestfulController-not-behaving-as-expected-tp4653396.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected

David Estes
This is not a bug. You are expecting your JSON payload to map to params. I’m pretty sure to get the JSON payload you have to use request.JSON as it is treated separately than params.

On Jan 17, 2014, at 8:01 AM, Sergio Michels <[hidden email]> wrote:

I've created a similar issue, a while ago: http://jira.grails.org/browse/GRAILS-10431

--
Sérgio Michels


On Fri, Jan 17, 2014 at 7:02 AM, jondo_w <[hidden email]> wrote:
I'm trying to setup a RestfulController based backend to an Ember app.

Some specifics:
Grails 2.3.4
Ember + Ember-RESTless

I've tried to follow the documentation in
http://grails.org/doc/latest/guide/webServices.html#restfulControllers as
best as I know how, but for some reason when doing a POST to create a new
record, I'm not getting the default behaviour working as expected.

My code is as follows:

Structure of JSON submitted in AJAX request:

{"serviceProviderType":{"name":"Assessor","image":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACuElEQVR42o2Ta0jTURjGB5JupUWN0EBIZkzn1OWGy+FMN3dxuSGmg1VTl5o2rbxsS526rc1LJs68MfNDlhB+LqzUIvP2n85JIFZmRRHhBz8EgalB9bQJGSGmLzycw+E8v3Peh3NIpB1KqbT4HpaV5pJTqlfI8tp1X2GJkrSrkuj3/ZnS89pBrSRArXKCoTL+2t7E1x6kSA33/fP64SerWibFFTG8y9ICM6gV40huc0OUmYNt/T4CXefe8/0I0E3Av+QJKJkt2JPWhEDDU3CbppF8Rgu+JPU7k8c7FKy0U7YABLk1nwLze5HWOg5esxuhZgJHawkwbE6ktM8gNrMIx5U6sFTVSLjiwBFJccM/gIwLBpNKU+iWZaih7JpAhJUAq94JdsM06NcIhNfNIszmAs1EIMTqBk2U/WPDSBaV0SgZLTP7z3aCKq9CaKoWDPMoYhqnQDdPIsQ04QES4ORYILnUDOH1ZxDedIObKPHmofQhp1S8D9CNgXp1zHNlJ6Lq3Yism0a4dRI0sxMF9xbAFysgUiiRJEsHT5ACllQNDl88TPJLLFT4q3sQZJwA3TIJTuM0+PZZyLpfQux4jRDbHMQ3XaBHsH7SY7isSBZXHs05oWewYq3hkWw56YCo5GGwftBz4hSiPf2Ge/rO7nqO+HPlSKp7jAj7W0S1fcCxmlEEZnesB5yqnvFTNJRuBsc8Xb4m73BB1jWHCBsBieMV2KkaxCUKkWwdAKd7CdyeJXAcn8Fs+wj5rXmwE6SjmwBDZc16zmUjChvvQG0fANsTFDOajbB03Yiq7x0Mw19hHFmBfugLih8so/XRPHRG84u/b12jCcovrSjO0ur60rMu4qRUgdDQsLay5tvV8QIpdtJ/v4N3g6V3cEPWu0MweUavFhbeYHFxcXeAuBuuLfq2uoq1tbXdAXbSbyFNjWsoy4bmAAAAAElFTkSuQmCC"}}

My mapping in UrlMappings is as follows:

        "/service_provider_types"(resources: "serviceProviderType",
parseRequest: true)

(I've tried with parseRequest:true and without it specified, same result)


My controller class is as follows:
class ServiceProviderTypeController extends
RestfulController<ServiceProviderType> {

    static responseFormats = ['json', 'xml'];

    ServiceProviderTypeController() {
        super(ServiceProviderType);
    }

}


My ServiceProviderType class is as follows:
class ServiceProviderType {

    String name;
    byte[] image;

    static constraints = {
        name(nullable: false)
        image(nullable: true)
    }
}

Now aside from the fact that my Ajax call sends a String image and the
domain class field is a byte[], the issue I'm having is that neither name
nor image is coming through in the request params. If I set a breakpoint in
RestfulController at this line, it hits the line as expected, but the params
are not as expected:

    @Transactional
    def save() {
        def instance = createResource(getParametersToBind())

        ...
    }

The getParametersToBind() has the action: 'delete', and the controller:
'serviceProviderType' and the RestfulController instance is of the correct
subclass, so my mapping is working.

But everything fails because the params do not contain the expected content.
I *think* I'm doing everything correctly, so what am I not doing correctly.
Any ideas appreciated. :-(

Thanks,
Darryl



--
View this message in context: http://grails.1312388.n4.nabble.com/RestfulController-not-behaving-as-expected-tp4653396.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected

jondo_w
Hi David,

That's not what the code says. If you look at the snippet I pasted at the end of my original post, that's directly from the RestfulController code. The getParametersToBind() call actually does use 'params'. Hence my confusion. Also if I override save(), request.JSON is null in my save method. So I'm stumped.

Thanks,
Darryl
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected

Owen Rubel
If you have time to wait, the Grails API Toolkit should be launching by Feb 1st (though perhaps without API Chaining).

And it will be able to handle it with a simple Annotation:

@Api(method=[POST],apiRoles=['ROLE_ADMIN','ROLE_USER'])

and a 'respond' in your method.

Nothing more. It relys on data returned and data requested (not on a one-to-one relationship between GORM table and controllers) so you can mix data however you want.
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected

Owen Rubel
By the way, here is the project codebase if you want to test it out ahead of time. Already working on the docs in the wiki...

https://github.com/orubel/grails-api-toolkit
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected - SOLVED!

jondo_w
In reply to this post by jondo_w
The solution to this problem was in the structure of the JSON being submitted to the RestfulController from my Ember app. It was submitting it embedded, as per the JSON I indicated in my OP.

{"serviceProviderType":{"name":"Assessor","image":"data:image ..... }

After I removed the "serviceProviderType" key and made the JSON flatter, ie:

{"name":"Assessor","image":"data:image........ }

it all worked as expected. The REST library I was using in Ember (ember-restless) embeds objects by default when serializing them. I had to override its default behaviour and instead make it non-embedded.

HTH.
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected - SOLVED!

Owen Rubel
Yep, the respond method in grails does that too. Thats why in the Grails API Toolkit, it removes this top layer so all return data is just as it need to be without any unexpected container. It should be a direct reflection of api docs.
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected - SOLVED!

ganta
Thanks Owen,

Your plugin helped me figure out how to get the request JSON onto the params object.  I built my Grails 2.2 api with the luxury of parseRequest putting the JSON on params for me, but Grails 2.3 has eliminated it.

I would be using your api plugin, but I am currently staying away from Spring Security.  If you had the option to omit Roles from the API annotations, I probably would've spent the time to integrate it.  I am handling roles via my own implementation of OAuth.
Reply | Threaded
Open this post in threaded view
|

Re: RestfulController not behaving as expected - SOLVED!

Owen Rubel
Yeah I'm a big fan of secure by default and securing your api. The next version (coming soon) has better abstraction and simplifies the anno, removes all bootstrapping, and further abstracts the object so roles are optional.

Owen Rubel
415-971-0976
[hidden email]


On Wed, Apr 2, 2014 at 11:32 PM, ganta <[hidden email]> wrote:
Thanks Owen,

Your plugin helped me figure out how to get the request JSON onto the params
object.  I built my Grails 2.2 api with the luxury of parseRequest putting
the JSON on params for me, but Grails 2.3 has eliminated it.

I would be using your api plugin, but I am currently staying away from
Spring Security.  If you had the option to omit Roles from the API
annotations, I probably would've spent the time to integrate it.  I am
handling roles via my own implementation of OAuth.



--
View this message in context: http://grails.1312388.n4.nabble.com/RestfulController-not-behaving-as-expected-SOLVED-tp4653396p4655700.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email