mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-23 01:26:46 +00:00
Now sending data using request body, instead of URL parameters (fixes #7)
Small fixes that I should have done earlier
This commit is contained in:
parent
2714c4c9e0
commit
f394f30c17
@ -6,7 +6,7 @@ import spark.Response;
|
|||||||
|
|
||||||
public class Routes {
|
public class Routes {
|
||||||
|
|
||||||
private static UrlRepository urlRepository;
|
private static final UrlRepository urlRepository;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
urlRepository = new UrlRepository();
|
urlRepository = new UrlRepository();
|
||||||
@ -17,8 +17,10 @@ public class Routes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String addUrl(Request req, Response res) {
|
public static String addUrl(Request req, Response res) {
|
||||||
String longUrl = req.queryParams("long");
|
var body = req.body();
|
||||||
String shortUrl = req.queryParams("short");
|
var split = body.split(";");
|
||||||
|
String longUrl = split[0];
|
||||||
|
String shortUrl = split[1];
|
||||||
|
|
||||||
if (shortUrl == null || shortUrl.isBlank()) {
|
if (shortUrl == null || shortUrl.isBlank()) {
|
||||||
shortUrl = Utils.randomString();
|
shortUrl = Utils.randomString();
|
||||||
@ -43,7 +45,7 @@ public class Routes {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
res.redirect(longUrlOpt.get());
|
res.redirect(longUrlOpt.get(), HttpStatus.PERMANENT_REDIRECT_308);
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -61,17 +61,18 @@ const submitForm = () => {
|
|||||||
const longUrl = form.elements["longUrl"];
|
const longUrl = form.elements["longUrl"];
|
||||||
const shortUrl = form.elements["shortUrl"];
|
const shortUrl = form.elements["shortUrl"];
|
||||||
|
|
||||||
const url = `/api/new?long=${longUrl.value}&short=${shortUrl.value}`;
|
const url = `/api/new`;
|
||||||
|
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: "POST"
|
method: "POST",
|
||||||
|
body: `${longUrl.value};${shortUrl.value}`
|
||||||
})
|
})
|
||||||
.then(_ => {
|
.then(_ => {
|
||||||
longUrl.value = "";
|
longUrl.value = "";
|
||||||
shortUrl.value = "";
|
shortUrl.value = "";
|
||||||
|
|
||||||
refreshData();
|
refreshData();
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user