# Get error 401 on images on WP Rest API

**URL:** <https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912>\
**Category:** ionic-v3\
**Created:** [July 10, 2018, 4:11pm UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912 "2018-07-10T16:11:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdinandferdinandfe](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ferdinandferdinandfe/32/119927_2.png) [@ferdinandferdinandfe](https://forum.ionicframework.com/u/ferdinandferdinandfe)\
**Post date:** [July 10, 2018, 4:11pm UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912/1 "2018-07-10T16:11:53Z")

</div>

Hello,

I’m using a basic authentication in my app to load articles of my wordpress which is locked with authentication.  
I’ve used HttpHeaders but I don’t know why, every image returns a 401 error (unauthorized).

I thought it was a CORS issue until I tried on every browser, device and used a plugin to allow CORS on Chrome.

Here is the code that get the articles :

```auto
get(query: string = '') {

    let username = "test";
    let password = "test";
    const httpOptions = {
      headers: new HttpHeaders({
        'Authorization': 'Basic ' +
          btoa(username + ':' + password)
      })
    };

    return this.http.get(this.API_URL + query, httpOptions);
  }

```

and the code in home.html that fails to load  
`<img src="{{ item._embedded['wp:featuredmedia'][0].media_details.sizes.medium.source_url }}" />`

It always shows this

 ![09](https://global.discourse-cdn.com/ionicframework/original/3X/b/e/be253d1b6960be99f316948250f793479ab9585e.png)

Can anyone help ? Thank you

---

<div class="post-metadata">

**Author:** ![rodrigoguariento](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rodrigoguariento/32/120378_2.png) [@rodrigoguariento](https://forum.ionicframework.com/u/rodrigoguariento)\
**Post date:** [July 10, 2018, 7:01pm UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912/2 "2018-07-10T19:01:26Z")

</div>

Try:

```auto
get(query: string = '') {

    let username = "test";
    let password = "test";
    let headers:HttpHeaders = new HttpHeaders({
        'Authorization': 'Basic ' +
          btoa(username + ':' + password)
    });

    return this.http.get(this.API_URL + query, { headers: headers } );
  }

```

---

<div class="post-metadata">

**Author:** ![ferdinandferdinandfe](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ferdinandferdinandfe/32/119927_2.png) [@ferdinandferdinandfe](https://forum.ionicframework.com/u/ferdinandferdinandfe)\
**Post date:** [July 10, 2018, 8:24pm UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912/3 "2018-07-10T20:24:35Z")

</div>

> [@rodrigoguariento](#):
>
> let headers:HttpHeaders = new HttpHeaders({ ‘Authorization’: 'Basic ’ + btoa(username + ‘:’ + password) });

hello, thank you but actually the headers work, it’s just with the images

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [July 11, 2018, 4:39am UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912/4 "2018-07-11T04:39:30Z")

</div>

Your `get` method with its authorization is totally irrelevant to the way you are fetching images. If you insist on protecting the images behind an authorization barrier, then you need to be responsible for managing the image fetching yourself. You cannot expect `<img src="protected-url">` to know how to deal with authorization.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:30pm UTC](https://forum.ionicframework.com/t/get-error-401-on-images-on-wp-rest-api/134912/5 "2021-07-19T21:30:08Z")

</div>

Closed on July 19, 2021, 9:30pm UTC


