Skip to content

fix(reddit): use browser-like headers to fix HTTP 403 from urllib#199

Open
francocarballar wants to merge 1 commit intomvanhorn:mainfrom
francocarballar:fix/reddit-public-403-browser-headers
Open

fix(reddit): use browser-like headers to fix HTTP 403 from urllib#199
francocarballar wants to merge 1 commit intomvanhorn:mainfrom
francocarballar:fix/reddit-public-403-browser-headers

Conversation

@francocarballar
Copy link
Copy Markdown

Problem

reddit_public.py uses Python's urllib with a generic User-Agent: last30days/3.0 (research tool). Reddit's servers block this with HTTP 403 consistently, resulting in 0 Reddit results for every query.

curl with the same URL returns 200 — the issue is specific to urllib's minimal request fingerprint, not Reddit's API availability.

Root cause

Reddit detects and blocks non-browser HTTP clients based on the combination of User-Agent + missing browser headers (Accept-Language, Accept-Encoding, Connection).

Fix

  1. Replace the generic USER_AGENT with a standard browser User-Agent string.
  2. Add Accept-Language, Accept-Encoding, and Connection headers to match a real browser request.
  3. Add gzip decompression — Reddit now returns compressed responses when Accept-Encoding: gzip is sent, so the response body must be decompressed before JSON parsing.

Verification

Tested locally: before the fix every request returned [RedditPublic] 403 forbidden. After the fix, queries return 9+ results as expected.

# Before
[RedditPublic] 403 forbidden: https://www.reddit.com/search.json?q=...

# After
✓ Research complete (13.8s) - Reddit: 9 threads

Python's urllib with a generic User-Agent gets blocked by Reddit
with HTTP 403. Switching to a browser User-Agent and adding
Accept-Language, Accept-Encoding and Connection headers fixes it.

Also adds gzip decompression since Reddit now returns compressed
responses when Accept-Encoding: gzip is sent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant