Download a url (with a password)

How do I download a file from a URL, if a username and password are required? For example, in MATLAB, I can do

url = 'https://n5eil01u.ecs.nsidc.org/DP4/ICEBRIDGE/IRMCR2.001/2010.03.24/IRMCR2_20100324_01.csv';
urlread(url,'Username','chadagreene','Password','notactuallymypassword')

and that saves a file called IRMCR2_20100324_01.csv to my local directory. That’s exactly what I want. Now how do I do it in Julia?

Better to use Downloads which is a standard library. Something like

using Downloads: download
file = download(url)

You can create a ~/.netrc file with a username and password. You can also put the username and password in the URL.

3 Likes

In particular, see http - How to add login credentials to URL - Stack Overflow

Please do not post code generated with ChatGTP if you have not tested it (it may be correct, incorrect or not even valid julia). The good thing about this forum is that actual julia users share their knowledge and experience.

6 Likes

6 posts were split to a new topic: Download with password (digression)

Another security-related item that isn’t explicitly mentioned yet but definitely deserves a mention is that if you make a call like

url = 'https://n5eil01u.ecs.nsidc.org/DP4/ICEBRIDGE/IRMCR2.001/2010.03.24/IRMCR2_20100324_01.csv';
urlread(url,'Username','chadagreene','Password','notactuallymypassword')

then that line is going to be stored in your REPL history (in any language). So at a bare minimum you should be using some external file to store passwords and keys and load those programmatically.

2 Likes

@chadagreene in the SpaceLiDAR.jl package we write a .netrc file using this function:

This file will allow NASA EarthData and DAACs to authenticate