voliva / angular2-interceptors

79 stars 20 forks source link

Body is empty on request when setting requestOptions.body = JSON.stringify({x:1}); #14

Closed barocsi closed 7 years ago

barocsi commented 7 years ago

With this module my JSON Body is stripped out from request using

requestOptions.body = JSON.stringify({any object});
return this.http.request(path, requestOptions)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return; //response.json();
                }
            });

easy to reproduce, the original Angular 2 http provider works.

voliva commented 7 years ago

Yes, I incorrectly overriden requestOptions.body and requestOptions.method. Should be fixed now in version 1.2.3 (here in git is branch hotfix/bodyMethodOverride)

Thanks a lot for pointing out these issues.

barocsi commented 7 years ago

Ok, I have just discovered the same in around interceptor-service.js line 86

voliva commented 7 years ago

I checked the original Angular Http code here and the behaviour is the same: If the first parameter is a Request instead of an url (string), then the options parameter is ignored, because the Request object already has all options. To keep consitency with the vanilla Http service I will keep it like that.

barocsi commented 7 years ago

Nope. The code below indicates that request.body is not being passed. So the problem is not the concept but the implementation.

InterceptorService.prototype.request = function (url, options) {
        options = options || {};
        var responseObservable;
        if (typeof url === 'string') {
            responseObservable = this.httpRequest({
                url: url,
                options: options,
                interceptorOptions: options.interceptorOptions || {}
            });
        }
        else if (url instanceof http_1.Request) {
            var request = url;
            responseObservable = this.httpRequest({
                url: request.url,
                options: {
                    method: request.method,
                    headers: request.headers,
                    url: request.url,
                    withCredentials: request.withCredentials,
                    responseType: request.responseType
                },
                interceptorOptions: options.interceptorOptions || {}
            });
        }
        else {
            throw new Error('First argument must be a url string or Request instance.');
        }
        return responseObservable;
    };
voliva commented 7 years ago

My bad, that's true, I'll fix that. Edit: Updated. Try again with 1.2.4?

barocsi commented 7 years ago

Works!