HTTP
File Download
Internet
Web Browsing
Tech Guide

How to download a file over HTTP

Codemia Knowledge Hub ·

From a terminal, run curl -O http://example.com/file.zip or wget http://example.com/file.zip. In code, send an HTTP GET request and write the response body to a file, for example with Python's requests or Node.js axios.

Under the hood, your browser or tool sends a GET request to the server hosting the file. The server responds with the file content, plus metadata in the response headers such as the file type and size.

Command line: curl and wget

curl supports many protocols, including HTTP, HTTPS and FTP, and ships with most Unix-based systems (Linux, macOS).

bash
  curl -O http://example.com/file.zip

-O saves the file under the same name as in the URL.

wget is designed for non-interactive downloads from the web, so it is a little simpler for basic file downloads:

bash
  wget http://example.com/file.zip

Python with requests

requests is the most common Python library for HTTP:

python
1  import requests
2  url = 'http://example.com/file.zip'
3  r = requests.get(url)
4  with open('file.zip', 'wb') as f:
5      f.write(r.content)

This fetches the file and writes it to disk.

Node.js with axios

javascript
1  const axios = require('axios');
2  const fs = require('fs');
3  const url = 'http://example.com/file.zip';
4
5  axios({
6    method: 'get',
7    url: url,
8    responseType: 'stream'
9  }).then(function (response) {
10    response.data.pipe(fs.createWriteStream('file.zip'));
11  });

responseType: 'stream' lets you pipe the response straight into a file.

Comparing the options

Tool or LanguageCommand/CodeProsCons
curlcurl -O http://example.com/file.zipHighly configurable, versatileSteeper learning curve
wgetwget http://example.com/file.zipSimpler for direct downloadsLess flexible than curl
Pythonrequests.get(url)Simple syntax, powerfulRequires external lib
Node.jsaxios({method: 'get', url: url, responseType: 'stream'})Asynchronous support, modernSlightly verbose

Practical checks

  • File size: before a large download, check the Content-Length header if available to make sure there is enough storage.
  • HTTPS: prefer HTTPS over HTTP for integrity and privacy.
  • Errors: handle timeouts, network failures and corrupt downloads.
  • Rate limits: when downloading from an API, respect its rate limits and usage guidelines.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design