kotlinRenderer.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Fence } from '@/components/Fence';
  2. const KotlinRequestRenderer = ({ url, bodyMethod, bodyParams }) => {
  3. const httpBody = bodyParams;
  4. const code = `
  5. import java.io.OutputStream
  6. import java.net.HttpURLConnection
  7. import java.net.URL
  8. fun main() {
  9. try {
  10. val url = "${url}"
  11. val jsonInputString = """
  12. {
  13. "jsonrpc": "2.0",
  14. "id": 1,
  15. "method": "${bodyMethod}",
  16. "params": ${JSON.stringify(httpBody, null, 2).replace(/\n/g, '\n ')}
  17. }
  18. """
  19. // Create a URL object from the string URL
  20. val obj = URL(url)
  21. val con = obj.openConnection() as HttpURLConnection
  22. // Set the HTTP method to POST
  23. con.requestMethod = "POST"
  24. // Set the headers
  25. con.setRequestProperty("Content-Type", "application/json")
  26. // Enable input and output streams
  27. con.doOutput = true
  28. // Write the request body (JSON)
  29. con.outputStream.use { os: OutputStream ->
  30. val input = jsonInputString.toByteArray(Charsets.UTF_8)
  31. os.write(input, 0, input.size)
  32. }
  33. // Get the response code
  34. val responseCode = con.responseCode
  35. println("Response Code: \$responseCode")
  36. } catch (e: Exception) {
  37. e.printStackTrace()
  38. }
  39. }
  40. `;
  41. return (
  42. <Fence className="w-full" language="kotlin">
  43. {code}
  44. </Fence>
  45. );
  46. };
  47. export default KotlinRequestRenderer;