0

The image I want to download returns 403 when I tried to download with javascript

but I can download image with python code like below. It adds referer to headers and downloads it.

req = urllib.request.Request(image_url, headers={"Referer": referer_url})
with urllib.request.urlopen(req) as response, open("image.jpg", "wb") as outfile:
    shutil.copyfileobj(response, outfile)

Is javascript can download image like that ?

Javascript code I am trying with. Its react-native-fetch-blob libaray

export const saveToonImageToLocal = (url, imageName) => {
  return RNFetchBlob
    .config({
      path: dirs.DocumentDir + `${imageName}.jpg`,
      appendExt: 'jpg'
    })
    .fetch('GET', url, {})
    .then((res)=>{
      return res.path();
    })
    .catch((e)=>{
      console.log('error occurend during saving images');
    })
};
fiddlest
  • 1,262
  • 2
  • 17
  • 42
  • Can you add the Javascript you're using to your question? – wogsland Mar 29 '17 at 18:21
  • Possible duplicate of [Set referer for xml.HTTP.Request?](http://stackoverflow.com/questions/27218525/set-referer-for-xml-http-request) – gyre Mar 29 '17 at 18:24

1 Answers1

0

You can load images using javascript ajax calls and passing headers in it:

  $.ajax({
     type:'GET',
     url:'https://example.com/someimage.png',
     headers: {'Referer': "http://somepage.com"},
     success:function(data){
        $('#someImage').attr('src',data);
     }
  });
Shakti Phartiyal
  • 6,156
  • 3
  • 25
  • 46