惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
博客园_首页
T
Tailwind CSS Blog
The Cloudflare Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why Your API Calls Break: A Developer's Guide to URL Enco...
zhihu wu · 2026-06-19 · via DEV Community

zhihu wu

Why Your API Calls Break: A Developer's Guide to URL Encoding

URL encoding is one of those things that seems trivial until it isn't. You fire off an API call, the server returns a 400, and you stare at the URL wondering what went wrong. Nine times out of ten, the answer is encoding.

Here are the three URL encoding mistakes that cost developers hours of debugging — and how to fix them in 30 seconds.

1. The Double-Encoding Trap

This is the most insidious bug. You encode a URL once, pass it through a framework or middleware that encodes it again, and suddenly hello%20world becomes hello%2520world (the % got encoded to %25).

The symptom: Downstream services receive garbled data. OAuth callback URLs fail silently. API gateways forward corrupted parameters.

The fix: Before encoding, decode first to establish a clean baseline:

// Bad: might double-encode
const url = "https://api.example.com?q=" + encodeURIComponent(input);

// Good: decode first if input might already be encoded
const clean = decodeURIComponent(input); // throws if malformed
const url = "https://api.example.com?q=" + encodeURIComponent(clean);

If you suspect double-encoding, look for %25 in your output — that's the smoking gun (% encoded as %25).

2. The + vs %20 Confusion

Both represent a space, but they're NOT interchangeable across contexts.

Context Encoding Works?
Query string (form data) +
Query string (manual) %20
Path segment + ❌ Broken
Fragment identifier + ❌ Broken
Everywhere %20 ✅ Always

The fix: Use %20 when you're not sure. It works universally. Use + only for application/x-www-form-urlencoded form submissions.

JavaScript's encodeURIComponent() outputs %20 — the safe choice. Python's urllib.parse.quote() does the same by default.

3. Encoding the Wrong Thing

The most common pattern I see in code reviews:

// Wrong: encodeURI on a full URL
const url = encodeURI("https://api.example.com/search?q=hello world");
// Result: "https://api.example.com/search?q=hello%20world" (works, but fragile)

// Wrong: encodeURIComponent on a full URL
const url = encodeURIComponent("https://api.example.com/search?q=hello world");
// Result: "https%3A%2F%2Fapi.example.com%2Fsearch%3Fq%3Dhello%20world" (broken)

The rule: encodeURI() for entire URLs (preserves ://, ?, &, #). encodeURIComponent() for individual parameter values. Never mix them up.

Quick Debugging Workflow

When an API call fails with encoding issues, I use this 3-step process:

  1. Decode the URL to see what's actually there — strip away the percent-encoding and read the raw string
  2. Re-encode a clean version — start fresh from the decoded text
  3. Compare — if the re-encoded version works but the original doesn't, you had double-encoding or mixed encoding

I keep a free URL Encoder/Decoder bookmarked for exactly this: paste a broken URL, hit Decode, and instantly see what went wrong. No signup, no server uploads — everything runs in your browser.

🔗 Try it: codetoolbox.pro/tools/url-encoder.html

Modern Alternative: URLSearchParams

If you're writing modern JavaScript, the URL and URLSearchParams APIs handle encoding automatically:

const params = new URLSearchParams({ q: "hello world", page: "1" });
const url = "https://api.example.com/search?" + params.toString();
// "https://api.example.com/search?q=hello+world&page=1"

This is cleaner, safer, and immune to double-encoding. Use it for new code.


What URL encoding bugs have burned you in production? Drop a comment — I'm collecting war stories.