Last active
December 19, 2019 08:41
-
-
Save CraicOverflow89/07770daa04a5a0e81ce978d33a50be1c to your computer and use it in GitHub Desktop.
Example of using POST request to javascript-minifier.com to minify a javascript file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
import java.io.InputStreamReader | |
import java.io.OutputStreamWriter | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import java.net.URLEncoder | |
fun main() { | |
// Input File | |
val input = File("/path/to/file.js") | |
// Create Content | |
val content = StringBuilder().apply { | |
append(URLEncoder.encode("input", "UTF-8")) | |
append("=") | |
append(URLEncoder.encode(input.readText(), "UTF-8")) | |
}.toString() | |
// Create Request | |
val request = (URL("https://javascript-minifier.com/raw").openConnection() as HttpURLConnection).apply { | |
requestMethod = "POST" | |
doOutput = true | |
setRequestProperty("Content-Type", "application/x-www-form-urlencoded") | |
setRequestProperty("charset", "utf-8") | |
setRequestProperty("Content-Length", content.length.toString()) | |
OutputStreamWriter(outputStream).apply { | |
write(content) | |
flush() | |
} | |
} | |
// Parse Response | |
if(request.responseCode == 200) { | |
// Print Result | |
println(InputStreamReader(request.inputStream).readText()) | |
} | |
// Handle Error | |
else println("Error: ${request.responseCode} ${request.responseMessage}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment