From f394f30c1782f93a4bab78637e205d430a0ab3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemek=20Draga=C5=84czuk?= Date: Mon, 9 Nov 2020 10:30:30 +0100 Subject: [PATCH] Now sending data using request body, instead of URL parameters (fixes #7) Small fixes that I should have done earlier --- src/main/java/tk/draganczuk/url/Routes.java | 10 ++++++---- src/main/resources/public/js/main.js | 15 ++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/tk/draganczuk/url/Routes.java b/src/main/java/tk/draganczuk/url/Routes.java index da2765e..ae070a3 100644 --- a/src/main/java/tk/draganczuk/url/Routes.java +++ b/src/main/java/tk/draganczuk/url/Routes.java @@ -6,7 +6,7 @@ import spark.Response; public class Routes { - private static UrlRepository urlRepository; + private static final UrlRepository urlRepository; static { urlRepository = new UrlRepository(); @@ -17,8 +17,10 @@ public class Routes { } public static String addUrl(Request req, Response res) { - String longUrl = req.queryParams("long"); - String shortUrl = req.queryParams("short"); + var body = req.body(); + var split = body.split(";"); + String longUrl = split[0]; + String shortUrl = split[1]; if (shortUrl == null || shortUrl.isBlank()) { shortUrl = Utils.randomString(); @@ -43,7 +45,7 @@ public class Routes { return ""; } - res.redirect(longUrlOpt.get()); + res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308); return ""; } diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index a3c335a..473ecad 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -61,17 +61,18 @@ const submitForm = () => { const longUrl = form.elements["longUrl"]; const shortUrl = form.elements["shortUrl"]; - const url = `/api/new?long=${longUrl.value}&short=${shortUrl.value}`; + const url = `/api/new`; fetch(url, { - method: "POST" + method: "POST", + body: `${longUrl.value};${shortUrl.value}` }) - .then(_ => { - longUrl.value = ""; - shortUrl.value = ""; + .then(_ => { + longUrl.value = ""; + shortUrl.value = ""; - refreshData(); - }); + refreshData(); + }); };