I am using an python-eve-API (short called eve
) to access a MongoDB. After inserting some sample data I am using Chrome's Postman to test the API.
Since eve provides additional information besides the requested data and HATEOAS directives as explained in the docs I just wondered about how to request the _items
-dictionary only (call it dictionary
if speaking in terms of Python and object
if speaking in terms of JSON or JavaScript).
So the sample request http://127.0.0.1:5000/packagings/
gives the response shown below from which I am only interested in the data contained in the _items
-dictionary/-object.
For sure, I can extract the desired data after receiving and storing the full response. However, is there a way to only request the data I am interested in in order to reduce additional data extraction resp. data handling once the data are received?
{
"_links": {
"self": {
"href": "packagings",
"title": "packagings"
},
"parent": {
"href": "/",
"title": "home"
}
},
"_meta": {
"max_results": 25,
"page": 1,
"total": 1
},
"_items": [
{
"diameter_dk": 0.0144,
"_created": "Tue, 17 Nov 2015 21:15:37 GMT",
"factor_fa": 2.1,
"_id": "564b98f955c40f29843128df",
"free_volume": 0.89,
"title": "raschigring10x10x0.5",
"_updated": "Tue, 17 Nov 2015 21:15:37 GMT",
"_links": {
"self": {
"href": "packagings/564b98f955c40f29843128df",
"title": "Packaging"
}
},
"specific_weight": 920,
"title_hr": "Raschig-Ring 10x10x0.5",
"specific_surface": 500,
"specific_number": 770000,
"_etag": "bcb4080b61028405babcd960196d27208c3eabd3"
}
]
}
You can disable HATEOAS by setting HATEOAS = False
in your configuration settings. That should greatly reduce the payload, making it more suitable for your use case.
EDIT: You can also choose to transform the response payload by hooking a callback function to the on_fetched_resource
event.
from eve import Eve
def on_fetched_resource(resource, response):
del(response['_links'])
del(response['_meta'])
# would result in an empty JSON document
# del(response['_items'])
app = Eve()
app.on_fetched_resource += on_fetched_resource
if __name__ == '__main__':
app.run()
Since the response is a dict (it's JSON after all), you would still need to provide a key for the documents array.