Román
Román

Reputation: 53

PUT request is not triggered (POST and GET work)

POST and GET method work perfectly but PUT method does not work.

I have a component with this function to update an Album data via Subscription:

onChangeTitle() {
    this.album.name = this.titleInput;
    const albumToEdit: IAlbumEdit = {
      'name': this.album.name,
      'description': this.album.description,
      'public': this.album.public
    };

    this.albumService.putAlbum(this.album.id, albumToEdit).subscribe(
      album => {
        this.album = album;
      },
      error => {
        this.handleError('no se ha podido modificar el álbum: ' + error);
        this.titleInput = this.album.name;
      }
    );
  }

The service has this function that return an Observable:

putAlbum(id, album: IAlbumEdit): Observable<IAlbum> {
    return this.http.put<IAlbum>(`${AppSettings.API_URL}/album/${id}`, album);
}

It is really easy but I don't understand why this httpclient put method is not triggered (I can see in network monitor in Firefox that any put is triggering).

Moreover I have an interceptor to add authorization header but it is not execute with PUT method.

My interceptor is this:

export class JwtInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // add authorization header with jwt token if available
    console.log('intercept');
    const currentUser = JSON.parse(localStorage.getItem('currentUser'));
    if (currentUser && currentUser.token) {
      request = request.clone({
        setHeaders: {
          Authorization: `Bearer ${currentUser.token}`
        }
      });
    }

    console.log(request);

    return next.handle(request);
  }
}

Upvotes: 0

Views: 238

Answers (1)

Rom&#225;n
Rom&#225;n

Reputation: 53

The issue has been fixed. The problem was I imported HTTP_INTERCEPTOR in more than one module. I fixed the issue importing it only in app.module.ts.

Upvotes: 1

Related Questions