dev.mccue/urlparameters
dev.mccue.urlparameters
bowbahdoe/urlparameters
urlparameters
provides the logic needed to read and write "URL parameters."
This covers both query parameters, often seen at the end of a URL (google.com?q=apples&track_id=123
), and the bodies of html form submissions (name=bob&age=98
).
It uses com.uwyn.urlencoder
to properly encode parameters for both situations.
Most websites eventually encode some information as query parameters inside a URL. urlparameters
lets you extract that information as well as produce such URLs.
It is also common for websites to accept information from a user via a form submission - more so if you server-side render HTML. Processing those form submissions means parsing those request bodies.
import java.net.URI;
import dev.mccue.urlparameters.UrlParameters;
void main() {
= URI.create("https://google.com?q=pear");
var url = UrlParameters.parse(url);
var params
System.out.println(params.firstValue("q").orElseThrow());
}
import dev.mccue.urlparameters.UrlParameters;
void main() {
= "name=jack&title=squire";
var body = UrlParameters.parse(body);
var params
// squire
System.out.println(params.firstValue("title").orElseThrow());
}
import java.util.List;
import java.net.URI;
import dev.mccue.urlparameters.UrlParameters;
import dev.mccue.urlparameters.UrlParameter;
void main() {
= new UrlParameters(List.of(
var params new UrlParameter("pokemon", "stantler"),
new UrlParameter("caught_in", "Pokemon Colosseum")
));
= URI.create("https://example.com?" + params);
var url
// https://example.com?pokemon=stantler&caught_in=Pokemon%20Colosseum
System.out.println(url);
}