Angular 2 forbidden error 403

2

my question is this: -Because I receive error 403 in firebug when wanting to represent an image ?. The application is being made with MEAN Stack.

getArtist(token, id: string){
        let headers = new Headers({
            'Content-Type' : 'application/json',
            'Authorization': token
        });
        let options = new RequestOptions({headers: headers});
        return this._http.get(this.url + 'getOneArtist/'+ id, options).map(res => res.json());
    }

That is the method for the artist service. And I want to put your corresponding image. The fact is that in principle everything has gone well so far, since I have headers in the back-end. CORS.

<div class="image-for-edit" *ngIf="artist.image && artist.image != 'null'">
    <img src="{{url + 'getImageArtist/' + artist.image}}">
</div>

Here is the HTML.

And finally, the controller method:

getArtist(){
    this._route.params.forEach((params: Params) => {
        let id = params['id'];

        this._artistService.getArtist(this.token, id).subscribe(
            response => {
                if(!response.artist) {
                    this._router.navigate(['/']);
                }else{
                    this.artist = response.artist;
                }
            },
            error => {
                var errorMessage = <any>error;
                if(errorMessage != null) {
                    console.log(error);
                }   
            }
        );
    });
}

Any clue as to why all the places where I want to put an image have not had a problem, and here?

Thanks in advance. Greetings.

    
asked by H. Díaz 24.07.2017 в 18:02
source

1 answer

0

I answer my same question, since I have found a solution. But I do not know if it was a patch to save the error for a while, or is done correctly. The fact is that I use a middleware for user authorization. And on the back end I had this:

File of routes in back-end:

router.get('/getImageArtist/:imageFile', md_auth.ensureAuth, ArtistController.getImageArtist);

That was one of the defined routes. According to what you can imagine, it is the route to be able to obtain the image file corresponding to each registered artist. Well here was the problem, you can include middlewares in the route, and I had put the user authorization middleware, which made, to my understanding, that could not get that file.

Once that bit is deleted, everything works and the image can be displayed.

I would like that, if someone has read this, and believes that I am not right or could perform another procedure, I would comment on it. I do not know if I'm totally right, although I see that it works, and I do not want to misinterpret other users.

    
answered by 24.07.2017 в 19:55