URL 인코딩/디코딩
URL 문자열을 인코딩 및 디코딩하여 특수 문자와 비-ASCII 텍스트를 안전하게 처리하며, 쿼리 파라미터 및 경로 전송에 적합합니다.
사용 방법
URL 인코딩은 특수 문자나 비-ASCII 문자를 %XX 형식으로 변환하여 URL 내에서 안전하게 전송할 수 있도록 합니다.
‘인코딩’ 버튼을 사용하여 텍스트를 URL에 안전하게 사용할 수 있는 형식으로 변환합니다(예: 쿼리 파라미터).
‘디코딩’ 버튼을 사용하여 %XX 시퀀스를 원래 문자로 복원합니다.
주요 사용 시나리오: 쿼리 파라미터 처리, 폼 데이터 전송, API 호출 등.
기능 특징
- 두 가지 인코딩 모드: encodeURIComponent 및 encodeURI
- 유연한 공백 처리: + 또는 %20으로 인코딩
- 실시간 변환 및 입력/출력 길이 통계 표시
- 복사/초기화 버튼과 양방향 패널 전송 기능
- 완전한 다국어 지원 및 DaisyUI 다중 테마 지원
URL 인코딩이란?
URL 인코딩은 문자를 %XX 퍼센트 인코딩 시퀀스로 변환하여 URL에서 안전하게 전송합니다.
encodeURIComponent는 단일 구성 요소(예: 단일 쿼리 매개변수 값)에 적합하며, 대부분의 영숫자가 아닌 문자를 인코딩합니다.
encodeURI는 전체 URI 문자열에 적합하며, ?, &, =, / 등 구조적 의미를 가진 예약 문자는 인코딩하지 않습니다.
주요 사용 사례
- 쿼리 매개변수 생성(검색 키워드, 필터 조건 등)
- GET/POST 폼 데이터 인코딩 및 전송
- API 요청 매개변수 인코딩
- 사용자 입력을 URL 경로 세그먼트에 안전하게 삽입
자주 묻는 질문과 주의사항
- encodeURI vs encodeURIComponent: 개별 매개변수는 component, 전체 링크는 URI 사용
- 공백과 +: application/x-www-form-urlencoded에서는 쿼리 문자열에서 공백을 +로 표현합니다.
- 중복 디코딩 방지: 여러 번 디코딩하면 데이터가 손상될 수 있습니다.
- 유효하지 않은 퍼센트 시퀀스는 오류를 발생시킵니다. 예외를 부드럽게 처리해야 합니다.
프로그래밍 언어에서 URL 인코딩/디코딩 하는 방법
JavaScript
인코딩
// Component (recommended for query values)
const encoded = encodeURIComponent(text);
// If you need '+' for spaces in query strings
const encodedPlus = encoded.replace(/%20/g, '+');
// Full URI
const encodedUri = encodeURI(url);
디코딩
// Treat '+' as space if needed
const input = plusAsSpace ? s.replace(/\+/g, ' ') : s;
// Component
const dec1 = decodeURIComponent(input);
// Fallback to decodeURI when input is a full URL
const dec2 = decodeURI(input);
PHP
인코딩
// Component (RFC 3986)
$encoded = rawurlencode($text);
// '+' for spaces (application/x-www-form-urlencoded)
$encodedPlus = str_replace('%20', '+', $encoded);
// Full query usage
$query = http_build_query(['q' => '你好 世界', 'type' => 'text']);
디코딩
// Component
$decoded = rawurldecode($s);
// If input uses '+' for spaces
$decodedPlus = urldecode($s);
Python
인코딩
from urllib.parse import quote, quote_plus
# Component (RFC 3986)
enc = quote(text, safe='')
# '+' for spaces (application/x-www-form-urlencoded)
enc_plus = quote_plus(text)
디코딩
from urllib.parse import unquote, unquote_plus
# Component
dec = unquote(s)
# Input with '+' for spaces
dec_plus = unquote_plus(s)
Go
인코딩
import (
"net/url"
)
// Query component
enc := url.QueryEscape(text) // spaces => +
// Path segment
// go1.8+
// url.PathEscape(text)
디코딩
import (
"net/url"
)
// Query component
dec, _ := url.QueryUnescape(s)
// Path segment
// url.PathUnescape(s)
Rust
인코딩
// Cargo.toml: urlencoding = "^2"
use urlencoding::{encode, encode_binary};
let enc = encode(text); // component; spaces => %20
let enc_plus = enc.replace("%20", "+"); // if you need '+' for spaces
디코딩
use urlencoding::decode;
// decode treats '+' as space
let dec = decode(s)?; // Result, _>
Java
인코딩
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
// Query param / form style (spaces => '+')
String enc = URLEncoder.encode(text, StandardCharsets.UTF_8);
// For RFC3986-like component encoding, replace '+' with %20 if needed
String encSpace = enc.replace("+", "%20");
디코딩
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
// URLDecoder treats '+' as space
String dec = URLDecoder.decode(s, StandardCharsets.UTF_8);