Img src path with header params to pass

Arjun picture Arjun · May 12, 2014 · Viewed 30.7k times · Source

I have an img tag in jsp page where the src path requires header parameters to pass to get the image. How can we achieve it?

Answer

flawyte picture flawyte · Jan 26, 2018

You can now use fetch() to add your headers and then load the result into an <img>:

const src = 'https://api.mywebsite.com/profiles/123/avatar';
const options = {
  headers: {
    'Some-Header': '...'
  }
};

fetch(src, options)
.then(res => res.blob())
.then(blob => {
  imgElement.src = URL.createObjectURL(blob);
});