perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

[Question] How to let sparkjava ignore the order of params in a link #1054

Closed Andre601 closed 5 years ago

Andre601 commented 5 years ago

I want to use sparkjava, to make a imageAPI, that returns a image with text, that was provided through the link itself. f.e. I would do http://localhost:4567/?text=some%20text and it would return a image, that has the text "some text" in it. But in my case will there be multiple params like who requested it, the color, the image, etc.

How can I let sparkjava listen to for example http://localhost:4567/img?text=some%20text&user=user but also for http://localhost:4567/img?user=user&text=some%20text

Would I have to set a get for every possible way, or is there a option, to let it listen for the params and ignore the order?

Like would the following code only work for http://localhost:4567/img?text=some%20text&user=user or would it also work for other the other example?

public void main(String[] args){
    get("/img?text=:text&user=:user", (req, res) -> {
        String text = req.params(":text");
        String user = req.params(":user");

        // We would return the image directly for that link
        return ImageUtil.newImage(text, user);
    });
}

I assume that this would only work for the provided format in the link (text first, then user) and not for the other one too.

Andre601 commented 5 years ago

I think I've found, what I was searching for.

Instead of using req.params(":something"); I can just use req.queryParams("something");