ngx-restangular
ngx-restangular copied to clipboard
Handling of getList with HTTP 204 and no body
Hey,
according to spec there must not be any body, when the server replies with HTTP 204. And in fact, chrome cancels a request, when there is data. This breaks the handling of restangular in this case:
ERROR Error: Response for getList SHOULD be an array and not an object or something else
This can be verified like this with he help of https://httpstat.us:
this.restangular.withConfig((RestangularConfigurer) => {
RestangularConfigurer.setBaseUrl('https://httpstat.us');
}).all('204').getList();
Back then in 2014 there was a small fix for handling of 204: https://github.com/mgonto/restangular/pull/561/commits/6565c86110cc9fe9930ce98ade926a3cd9e161f3
No idea if this ever worked, but int he current version data is of type Object, whenever the server sends no body and the check in the patch fails.
So my question before any pull request and whatsoever:
- Is this error message required at all?
- Is there any problem/side effect with always returning an array, when data isn't already one?
This works properly for me:
// support empty response for getList() calls (some APIs respond with 204 and empty body)
if (!isArray(data)) {
data = [];
}
Here some facts about my system, although I don't think they make any difference:
Linux (Debian 9) Version 70.0.3538.110 (Developer Build) built on Debian 9.6, running on Debian 9.6 (64-bit)
$ npm list | grep angular
@angular/[email protected]
@angular/[email protected]
@angular/[email protected]
@angular/[email protected]
@angular/[email protected]
@angular/[email protected]
@angular/[email protected]
@angular-devkit/[email protected]
@angular-devkit/[email protected]
@angular-devkit/[email protected] deduped
[email protected]
$ npm version
{ 'twttr-angular': '0.0.0',
npm: '6.4.1',
ares: '1.14.0',
cldr: '33.1',
http_parser: '2.8.0',
icu: '62.1',
modules: '64',
napi: '3',
nghttp2: '1.34.0',
node: '10.14.0',
openssl: '1.1.0j',
tz: '2018e',
unicode: '11.0',
uv: '1.23.2',
v8: '6.8.275.32-node.36',
zlib: '1.2.11' }
$ node --version
v10.14.0
Unfortunately, I can't find any strict definitions about rest pattern. From my understanding, collection endpoint shouldn't return 204 status. If there are no entities to return response should be an empty array. 204 status is used only for entities deletion. Will be great if you can share any docs or articles about 204 status response from collection endpoint