URL Encoding and Decoding in Free Pascal
When building a URL, parameter values may contain characters that need percent-encoding. Free Pascal provides HTTPEncode and HTTPDecode in the httpprotocol unit.
Use these routines on a parameter value rather than encoding an entire URL. For example, encode the search text before appending it to a query string:
uses
httpprotocol;
var
SearchText, EncodedText, OriginalText, URL: string;
begin
SearchText := 'Free Pascal & Lazarus';
EncodedText := HTTPEncode(SearchText);
URL := 'https://example.com/search?q=' + EncodedText;
writeln(URL);
OriginalText := HTTPDecode(EncodedText);
writeln(OriginalText);
end.
The routines are part of httpprotocol, which is also useful in HTTP server code. For an HTTPS server example, see the fphttpserver note in the Chinese version of this blog.
The Free Pascal documentation describes HTTPEncode as converting unsafe characters to percent-encoded form. The corresponding HTTPDecode reference documents how plus signs are handled; be aware of that behavior when decoding query or form data.
The example above is adapted from the Free Pascal Wiki article on URL encoding and decoding.