SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

How to return html body on PATCH/POST/PUT? #161

Closed javabr closed 7 years ago

javabr commented 7 years ago

I am trying to put together a solution with restheart and datatables - (HTML tables and forms at https://editor.datatables.net/manual/server).

The only problem I could not solve elegantly on putting those 2 together is the fact that datables asks for a JSON body when PUT/POST/PATCH are returning from the server.

How I could do that? I looked at Hooks and Transformers in RestHeart but it does not seem like they can change the response body.

ujibang commented 7 years ago

Hi @javabr

this is not currently possible because the write requests are not supposed to return data so their handlers chain do not include the ResponseSenderHandler (that is in charge of sending data back to the client).

However it indeed makes sense to allow a response transformer to define some response data; so I created the following improvement in our backlog https://softinstigate.atlassian.net/browse/RH-213 to be included in upcoming 3.0 release

I cannot commit on a release date but it should be by November.

javabr commented 7 years ago

Hi @ujibang , this is really great! Thank you for looking into this.

I think I can workaround my backlog and wait for this release.

The datatable web page above has some examples of return data for each type of request - For example, it describes what would be the response json for POSTs when multiple docs vs single documents are pushed from the datatables to the server.

In my use cases I will never be posting more than one document in a single request - datatable has an option to disable multiple selections / multiple deletions/ multiple edits.

Edit: could not subscribe to jira.

Although I understand this would not be a change specific for datatable clients, it is a good start and has real use cases to complement your analises and design.

I also managed to upload pictures using amazing .files features on restheart. Once again, I am missing a valid json response from the restheart API.

As I think about possible solutions for the problem, I think a customizable response (with some defaults) is the way to go. Therefore, every client implementor could customize the response to his/hers needs.

ujibang commented 7 years ago

for you information I just pushed a change that address this need (response transformers can be applied to write requests).

you can build it on your own or find the binaries from sonatype

I also created a transformer that returns the written data together with the old data (in case of an update) in the response body; see the code at WriteResultTransformer

javabr commented 7 years ago

Big Thank you! it is working!!!

Documenting how I have added this transformer to my transformers pipeline:

1) I have created the collection with the following: curl -i -X PUT \ -H "Authorization:Basic ***" \ -H "Content-Type:application/json" \ -d \ '{"rts":[{"name":"responseTransformer", "phase":"RESPONSE", "scope":"CHILDREN"}]}' \ 'http://localhost:8080/osso/test3'

2) Then I have configured the transformer into the restheart.yml:

That is it! Now all my responses are decorated by WriteResultTransformer.

I will probably write my on transformer latter on, based on the WriteResultTransformer, but this one will do for now!