🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The base64.urlsafe_b64encode function in Python's base64 module encodes binary data to Base64-encoded ASCII text using a URL-safe alphabet. This function is useful for encoding binary data into a textual format that can be safely included in URLs and filenames.
Table of Contents
- Introduction
base64.urlsafe_b64encodeFunction Syntax- Examples
- Basic Usage
- Encoding a String
- Encoding a File
- Real-World Use Case
- Conclusion
Introduction
The base64.urlsafe_b64encode function is part of the base64 module, which provides functions for encoding and decoding data using Base64. The URL-safe Base64 encoding replaces the + and / characters with - and _ respectively, making the encoded data safe for inclusion in URLs and filenames.
base64.urlsafe_b64encode Function Syntax
Here is how you use the base64.urlsafe_b64encode function:
import base64
encoded_data = base64.urlsafe_b64encode(data)
Parameters:
data: The binary data to encode. This must be abytesobject.
Returns:
- A
bytesobject containing the URL-safe Base64-encoded data.
Examples
Basic Usage
Encode binary data using base64.urlsafe_b64encode.
Example
import base64
data = b'hello world'
encoded_data = base64.urlsafe_b64encode(data)
print(f"Encoded data: {encoded_data}")
Output:
Encoded data: b'aGVsbG8gd29ybGQ='
Encoding a String
Encode a string by first converting it to bytes.
Example
import base64
string = 'hello world'
encoded_string = base64.urlsafe_b64encode(string.encode('utf-8'))
print(f"Encoded string: {encoded_string}")
Output:
Encoded string: b'aGVsbG8gd29ybGQ='
Encoding a File
Encode the contents of a file.
Example
import base64
with open('example.txt', 'rb') as file:
file_content = file.read()
encoded_content = base64.urlsafe_b64encode(file_content)
print(f"Encoded file content: {encoded_content}")
Output:
Encoded file content: b'...'
Real-World Use Case
Transmitting Binary Data in URLs
When sending binary data, such as an image, in a URL, it needs to be Base64 encoded in a URL-safe manner.
Example
import base64
import json
image_path = 'image.png'
with open(image_path, 'rb') as image_file:
image_content = image_file.read()
encoded_image = base64.urlsafe_b64encode(image_content).decode('utf-8')
url = f"https://example.com/upload?image={encoded_image}"
print(f"URL with encoded image: {url}")
Output:
URL with encoded image: https://example.com/upload?image=iVBORw0KGgoAAAANSUhEUgAA...
Conclusion
The base64.urlsafe_b64encode function is used for encoding binary data into Base64-encoded ASCII text using a URL-safe alphabet in Python. It provides a way to easily encode data for inclusion in URLs and filenames. By understanding how to use base64.urlsafe_b64encode, you can handle binary data more effectively and integrate it into text-based systems safely.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment