Connect to a service (HTTP)

Create a .netrc file

Note

Many services are protected by Basic authentication. When accessing these services in a web browser, you typically need to enter your credentials only once, and your browser will submit the Authorization header on subsequent requests. When accessing these services from the command line, you typically need to enter your credentials every time. Creating and using a .netrc file on your computer avoids this extra work.

To create (or append credentials to) a ~/.netrc file:

  1. Run, replacing HOSTNAME with the service’s hostname (e.g. collect.kingfisher.open-contracting.org), USERNAME with your username, and PASSWORD with your password:

    echo 'machine HOSTNAME
      login USERNAME
      password PASSWORD' >> ~/.netrc
    
  2. Check that only one section of the ~/.netrc file refers to the hostname, replacing HOSTNAME:

    $ grep -A2 HOSTNAME ~/.netrc
    machine myhostname
      login myuser
      password mypass
    

    If there are multiple sections or an incorrect password, you must correct the file in a text editor.

  3. Change the file’s permissions to be readable only by the owner:

    chmod 600 ~/.netrc
    
  4. Check the permissions:

    $ stat -f "%Sp" ~/.netrc
    -rw-------
    
  5. Test your configuration. For example, for Kingfisher Collect:

    $ curl -n https://collect.kingfisher.open-contracting.org/listprojects.json
    {"node_name": "ocp04", "status": "ok", "projects": ["kingfisher"]}