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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
博客园_首页
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
V
V2EX
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队

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
encodeURI vs encodeURIComponent: The JavaScript URL Encod...
zhihu wu · 2026-05-31 · via DEV Community

zhihu wu

Most JavaScript developers have been bitten by URL encoding at some point. You build a query string, pass it to fetch(), and suddenly your API returns 400. The culprit? You used encodeURI() when you should have used encodeURIComponent().

The Core Difference

JavaScript gives us two encoding functions, and they serve different purposes:

  • encodeURI() is designed for complete URLs. It keeps URL structure characters intact: /, ?, &, =, #, :, @, $, +, and ,. Use it when you have a full URL string and want to make it safe for transmission.

  • encodeURIComponent() is for individual parameter values. It encodes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ). This is what you want for query string values, path segments, and any data embedded in a URL.

The Classic Bug

Here's the mistake everyone makes at least once:

// WRONG — the @ and = break as literal URL characters
const email = "user@example.com";
const url = "https://api.example.com/search?email=" + encodeURI(email);
// Result: https://api.example.com/search?email=user@example.com
// The @ is interpreted as a URL authority separator!

The fix:

// RIGHT — all special chars are percent-encoded
const url = "https://api.example.com/search?email=" + encodeURIComponent(email);
// Result: https://api.example.com/search?email=user%40example.com

Double-Encoding: The Silent Killer

If you encode an already-encoded string, the % sign itself gets encoded to %25. hello%20world becomes hello%2520world. This is maddening to debug because it looks almost right. The fix: always decode first to see what you're actually working with.

The Modern Approach

In 2026, you rarely need to manually encode URLs. The URL and URLSearchParams APIs handle encoding automatically:

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

When You Still Need Manual Encoding

Sometimes you're debugging logs, comparing API responses, or working with encoded strings from external systems. That's when a quick encoder/decoder tool saves you from counting percent signs in your terminal.

Handy tool: I keep CodeToolbox URL Encoder bookmarked for those moments — paste any string, encode or decode instantly, all local processing so no data leaves your browser.


TL;DR: encodeURI() for full URLs. encodeURIComponent() for values. URLSearchParams for everything new. And if you see %25 in your output, you've double-encoded — decode and start over.