Re: encode/decode support for base64url
Florents Tselai <florents.tselai@gmail.com>
From: Florents Tselai <florents.tselai@gmail.com>
To: Aleksander Alekseev <aleksander@timescale.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>, Przemysław Sztoch <przemyslaw@sztoch.pl>, Daniel Gustafsson <daniel@yesql.se>
Date: 2025-03-08T22:27:42Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Add support for base64url encoding and decoding
- e1d917182c19 19 (unreleased) landed
> On 7 Mar 2025, at 4:40 PM, Aleksander Alekseev <aleksander@timescale.com> wrote:
>
> Hi,
>
>>> Sometimes support for base64url from RFC 4648 would be useful.
>>> Does anyone else need a patch like this?
>>
>> While not a frequent ask, it has been mentioned in the past. I think it would
>> make sense to add so please do submit a patch for it for consideration.
>
> IMO it would be nice to have.
>
> Would you like to submit such a patch or are you merely suggesting an
> idea for others to implement?
>
> --
> Best regards,
> Aleksander Alekseev
>
>
Just to confirm:
In a plan SQL flavor, we’re talking about something like this, correct?
CREATE FUNCTION base64url_encode(input bytea) RETURNS text AS $$
SELECT regexp_replace(
replace(replace(encode(input, 'base64'), '+', '-'), '/', '_'),
'=+$', '', 'g'
);
$$ LANGUAGE sql IMMUTABLE;
CREATE FUNCTION base64url_decode(input text) RETURNS bytea AS $$
SELECT decode(
rpad(replace(replace(input, '-', '+'), '_', '/'), (length(input) + 3) & ~3, '='),
'base64'
);
$$ LANGUAGE sql IMMUTABLE;
With minimal testing, this yields the same results with https://base64.guru/standards/base64url/encode
select base64url_encode('post+gres')
base64url_encode
------------------
cG9zdCtncmVz
(1 row)