com.uwyn.urlencoder

What is it

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.

Why use it

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.

Getting Started

Parse Query Params from a URL

import java.net.URI;

import dev.mccue.urlparameters.UrlParameters;

void main() {
   var url = URI.create("https://google.com?q=pear");
   var params = UrlParameters.parse(url);
   
   System.out.println(params.firstValue("q").orElseThrow());    
}

(Playground Link)

Parse Form Submission bodies

import dev.mccue.urlparameters.UrlParameters;

void main() {
   var body = "name=jack&title=squire";
   var params = UrlParameters.parse(body);
   
   // squire
   System.out.println(params.firstValue("title").orElseThrow());    
}

Generate a URL with Query Params

import java.util.List;
import java.net.URI;

import dev.mccue.urlparameters.UrlParameters;
import dev.mccue.urlparameters.UrlParameter;

void main() {
   var params = new UrlParameters(List.of(
       new UrlParameter("pokemon", "stantler"),
       new UrlParameter("caught_in", "Pokemon Colosseum")
   ));
   
   var url = URI.create("https://example.com?" + params);
   
   // https://example.com?pokemon=stantler&caught_in=Pokemon%20Colosseum
   System.out.println(url);    
}

<- Index