Danbooru

API 403 error when trying to load images in img tag

Posted under Bugs & Features

Basically what I'm doing is searching for images based on tags extracting the urls and trying to load the images to a react webpage using img tags.

The problem I'm having is that while some images load, there are some that give a 403 error.
For example: this one (nsfw) gives a 403: https://danbooru.donmai.us/data/f504b8e5499f624bde52472d9c5649e8.png

I'm using only 2 tags and neither of them are "loli" so i don't see why i'm getting a 403 (i'm using the api without login). Also if i inspect element and manually insert an image tag then i don't get a 403.

Any idea?

OP here. Found a workaround for anyone with similar issue. You just have to put referrerPolicy="no-referrer" in your image tag.
Relevant stackoverflow post: https://stackoverflow.com/questions/53536329/images-are-loading-okay-on-a-chrome-tab-but-returning-403-in-react-code

tldr of the post:
"The server might have a referer check, IOW: it doesn't want you using direct links to this resource, but maybe download and host yourself, so that there server is not unduly bashed."

"The server checks the referer sent by the browser and will not return the image when the referer is from a different domain. To prevent this, in many current browsers you can use the referrerpolicy attribute of the img tag to ask the browser to not send the referer:
<img referrerPolicy="no-referrer" src="https://www.host.net/image.png" />"

"NineBerry is correct, but it is best practice to add an explicit cross origin resource sharing permission to the headers in the server that is hosting the images (assuming you own that server), rather than ignoring the referer."

Not sure if the same origin referer check is intended by Danbooru but adding the referrerPolicy to the img tag works.

The referrer check was recently removed, not sure why you're still getting it. Hotlinking our images is fine for private or personal use, but it's discouraged for public-facing sites. It may be restricted further if it's abused.

1