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

推荐订阅源

有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
罗磊的独立博客
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
Docker
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
J
Java Code Geeks
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
美团技术团队
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
C
Check Point 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
Base64 vs Base64URL vs URL Encoding: When Should You Use ...
MOUSTAID Ham · 2026-04-24 · via DEV Community

Base64, Base64URL, and URL encoding are often confused because they all seem to “encode” data.

But they solve different problems.

Using the wrong one can break URLs, APIs, JWTs, image data, or browser behavior.

In this guide, we’ll compare:

  • Base64
  • Base64URL
  • URL encoding / percent encoding

You’ll learn what each one does, where each one is useful, and how to choose the right format.

For quick testing, you can use EncodingBase64 for Base64, Base64URL, URL encoding, hex, and image-to-Base64 conversions. If you need to reverse Base64 back into text, images, or files, DecodingBase64 is the matching decoder.


Quick answer

Use Base64 when you need to represent binary data as text.

Use Base64URL when Base64 data must safely appear inside URLs, JWTs, or URL tokens.

Use URL encoding when you need to safely place normal text inside a URL query string or path.

They are not interchangeable.


What is Base64?

Base64 converts bytes into readable ASCII characters.

Example:

Hello World

Enter fullscreen mode Exit fullscreen mode

becomes:

SGVsbG8gV29ybGQ=

Enter fullscreen mode Exit fullscreen mode

Base64 is commonly used for:

  • sending binary data in JSON
  • embedding small images
  • email attachments
  • API payloads
  • data URIs
  • basic text-safe representation

Base64 uses characters like:

A-Z
a-z
0-9
+
/
=

Enter fullscreen mode Exit fullscreen mode

The = character is padding.


When to use Base64

Use Base64 when the original data is binary or byte-based but needs to be represented as text.

Good use cases include:

1. Embedding small images

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." />

Enter fullscreen mode Exit fullscreen mode

2. Sending binary data in JSON

{
  "filename": "avatar.png",
  "content": "iVBORw0KGgoAAAANSUhEUgAA..."
}

Enter fullscreen mode Exit fullscreen mode

3. Encoding small files for transport

Some APIs accept file content as Base64 strings.

4. Encoding credentials in specific protocols

Basic authentication uses Base64 representation for:

username:password

Enter fullscreen mode Exit fullscreen mode

But remember: this is not secure by itself. It must be used over HTTPS.


When not to use Base64

Do not use Base64 for:

  • encryption
  • password protection
  • hiding secrets
  • large images when normal files work better
  • URL parameters without URL-safe conversion

Base64 increases data size, often by about one-third.

That is acceptable in many cases, but not always ideal.


What is Base64URL?

Base64URL is a URL-safe version of Base64.

Standard Base64 may contain:

+
/
=

Enter fullscreen mode Exit fullscreen mode

These characters can cause issues in URLs.

Base64URL replaces:

+ with -
/ with _

Enter fullscreen mode Exit fullscreen mode

Padding = is often removed.

Example standard Base64:

SGVsbG8+V29ybGQ/

Enter fullscreen mode Exit fullscreen mode

Base64URL version:

SGVsbG8-V29ybGQ_

Enter fullscreen mode Exit fullscreen mode

Base64URL is common in:

  • JWTs
  • URL-safe tokens
  • password reset links
  • email verification links
  • OAuth flows
  • compact identifiers

Why standard Base64 can break in URLs

Imagine this Base64 string:

abc+def/ghi==

Enter fullscreen mode Exit fullscreen mode

If placed directly inside a URL:

https://example.com/?token=abc+def/ghi==

Enter fullscreen mode Exit fullscreen mode

The + may be interpreted as a space in some URL contexts.

The / may be interpreted as a path separator.

The = can interfere with query parsing.

That is why Base64URL exists.

A safer token would look like:

abc-def_ghi

Enter fullscreen mode Exit fullscreen mode


Base64URL helper in JavaScript

Convert standard Base64 to Base64URL:

function toBase64Url(base64) {
  return base64
    .replace(/\+/g, "-")
    .replace(/\//g, "_")
    .replace(/=+$/g, "");
}

Enter fullscreen mode Exit fullscreen mode

Convert Base64URL back to standard Base64:

function fromBase64Url(base64Url) {
  let base64 = base64Url
    .replace(/-/g, "+")
    .replace(/_/g, "/");

  while (base64.length % 4 !== 0) {
    base64 += "=";
  }

  return base64;
}

Enter fullscreen mode Exit fullscreen mode

Example:

const base64 = "SGVsbG8gV29ybGQ=";
const base64Url = toBase64Url(base64);

console.log(base64Url);
// SGVsbG8gV29ybGQ

Enter fullscreen mode Exit fullscreen mode


What is URL encoding?

URL encoding, also called percent encoding, is different from Base64.

It makes text safe for URLs by replacing unsafe characters with % followed by hexadecimal values.

Example:

hello world

Enter fullscreen mode Exit fullscreen mode

becomes:

hello%20world

Enter fullscreen mode Exit fullscreen mode

Another example:

email@example.com

Enter fullscreen mode Exit fullscreen mode

becomes:

email%40example.com

Enter fullscreen mode Exit fullscreen mode

In JavaScript, you can use:

encodeURIComponent("hello world");
// hello%20world

Enter fullscreen mode Exit fullscreen mode

For a full URL, you may use:

encodeURI("https://example.com/search?q=hello world");
// https://example.com/search?q=hello%20world

Enter fullscreen mode Exit fullscreen mode


encodeURI vs encodeURIComponent

These two are often confused.

encodeURI()

Use it for a full URL:

encodeURI("https://example.com/search?q=hello world");

Enter fullscreen mode Exit fullscreen mode

It keeps URL structure characters like:

:
/
?
&
=

Enter fullscreen mode Exit fullscreen mode

encodeURIComponent()

Use it for a URL parameter value:

const query = encodeURIComponent("hello world & coffee");
const url = `https://example.com/search?q=${query}`;

console.log(url);
// https://example.com/search?q=hello%20world%20%26%20coffee

Enter fullscreen mode Exit fullscreen mode

Most of the time, if you are encoding a query parameter value, use encodeURIComponent().


Base64 vs URL encoding

Here is the key difference:

Feature Base64 URL Encoding
Main purpose Represent bytes as text Make URL text safe
Used for binary data Yes No
Used for query params Not directly Yes
Output style SGVsbG8= hello%20world
Reversible Yes Yes
Security feature No No

If you want to encode an image, use Base64.

If you want to safely put a search query inside a URL, use URL encoding.


Base64URL vs URL encoding

Base64URL is still Base64.

It is designed to make Base64 output safe for URLs.

URL encoding is designed to make normal text safe for URLs.

Example:

Original text:

Hello World

Enter fullscreen mode Exit fullscreen mode

Base64:

SGVsbG8gV29ybGQ=

Enter fullscreen mode Exit fullscreen mode

Base64URL:

SGVsbG8gV29ybGQ

Enter fullscreen mode Exit fullscreen mode

URL encoded:

Hello%20World

Enter fullscreen mode Exit fullscreen mode

They look different because they solve different problems.


Real-world examples

Example 1: API file upload

You have image bytes and need to send them in JSON.

Use Base64.

{
  "image": "iVBORw0KGgoAAAANSUhEUgAA..."
}

Enter fullscreen mode Exit fullscreen mode

Example 2: JWT token

JWTs use Base64URL.

A JWT looks like:

header.payload.signature

Enter fullscreen mode Exit fullscreen mode

Each part is Base64URL encoded.

Example 3: Search query

You want a URL like:

https://example.com/search?q=coffee & tea

Enter fullscreen mode Exit fullscreen mode

Use URL encoding:

https://example.com/search?q=coffee%20%26%20tea

Enter fullscreen mode Exit fullscreen mode

Example 4: Small inline SVG or PNG

Use Base64 or URL-encoded SVG depending on the case.

For small PNG/JPEG/WebP assets, Base64 is common.

For SVG, URL encoding can sometimes be more readable and efficient.


Common mistakes

Mistake 1: Using Base64 as encryption

Base64 can be decoded by anyone.

It does not protect data.

Mistake 2: Putting standard Base64 directly in URLs

Characters like +, /, and = can cause issues.

Use Base64URL or URL encode the value.

Mistake 3: Using URL encoding for binary files

URL encoding is not designed for raw binary file representation.

Use Base64 instead.

Mistake 4: Forgetting to decode in the right order

If data is URL encoded and Base64 encoded, the order matters.

For example:

  1. URL decode
  2. Base64 decode

or the reverse, depending on how the data was originally encoded.

Mistake 5: Not handling Unicode

JavaScript’s btoa() and atob() are not always Unicode-safe by themselves.

Use TextEncoder and TextDecoder when working with modern text.


Which one should you choose?

Use this simple rule:

Situation Use
Encode text or binary as ASCII Base64
Put Base64 safely in a URL Base64URL
Encode a URL query parameter URL encoding
Encode an image as text Base64
Encode a JWT payload Base64URL
Encode spaces and symbols in URLs URL encoding
Hide sensitive data None of these

For sensitive data, use proper encryption, hashing, HTTPS, and secure storage depending on the use case.


Final thoughts

Base64, Base64URL, and URL encoding are all useful, but they are not the same.

Base64 helps represent bytes as text.

Base64URL makes Base64 safer for URLs and tokens.

URL encoding makes normal URL text safe.

When debugging encoded data, first identify what kind of encoding you are dealing with. Then choose the right tool or function.

For browser-based testing, EncodingBase64 is useful for encoding workflows like Base64, Base64URL, URL encoding, image to Base64, and hex. For reverse workflows, DecodingBase64 helps decode Base64 back into text, images, hex, or files.

The main thing to remember:

Encoding is about representation. It is not the same as encryption.