0

Restheart returns an empty response for a document addition (with PUT or POST methods).

is it possible to modify the configuration so that it returns a valid json response:

Like {} for example ?

is there a way that restheart returns the id or objectId of the inserted document?

Versions :

  • restheart 3.0.0
  • mongoDb 3.4.1
jcheron
  • 193
  • 1
  • 9

2 Answers2

0

No, there's no such configuration. As per HTTP specifications, RESTHeart returns the result of a PUT or POST via HTTP headers. This makes sense, as those are all metadata. Generally speaking, the HTTP specs try to enforce a separation between data, usually in the response body, and metadata, usually in the HTTP headers.

For example, the following curl command posts a JSON document into a MongoDB collection:

$ curl -i -H "Content-Type: application/json" -X POST http://dbapi.io/db/coll -d '{"from":"mkjsix", "message": "MongoDB rocks!"}'

HTTP/1.1 201 Created
Server: nginx/1.9.15
Date: Tue, 23 Jan 2018 16:29:31 GMT
Content-Type: application/json
Content-Length: 0
Location: http://dbapi.io/db/coll/5a6762eb74efb71411471489
Connection: keep-alive
X-Powered-By: restheart.org
Access-Control-Expose-Headers: Location, ETag, Auth-Token, Auth-Token-Valid-Until, Auth-Token-Location, X-Powered-By
Access-Control-Allow-Origin: *
ETag: 5a6762ebc9e77c0007974479
Access-Control-Allow-Credentials: true

As you can see, the Location header contains the URI of the newly created document, which actually ends with the object id, as generated by MongoDB. If you instead use the PUT verb, then you are providing the id by yourself.

mturatti
  • 651
  • 5
  • 10
0

Thanks mturatti !

Too bad for me. I am using a RESTAdapter that I can customize in an EmberJS application.

The RESTAdapter is still waiting for a JSON response: I found a solution for that, by changing the dataType of Ajax queries from "JSON" to "*".

But the absence of id returned in JSON format is more problematic.

I would have to find a way to generate a JSON response by including the id of the inserted object retrieved from the Location header

From that in the headers,

Location: http://dbapi.io/db/coll/5a6762eb74efb71411471489

I would like to have in response body:

{"_id":"5a6762eb74efb71411471489"}

While waiting to find the solution, I use an ember addon to generate client-side uuid, which solves the problem

But it is regrettable not to use the id generated by mongo, the id of the inserted object is not a meta-data

jcheron
  • 193
  • 1
  • 9