Http.svelte 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <script>
  2. import { getClient, Body } from "@tauri-apps/api/http";
  3. let httpMethod = "GET";
  4. let httpUrl = "";
  5. let httpBody = "";
  6. export let onMessage;
  7. async function makeHttpRequest() {
  8. const client = await getClient()
  9. let method = httpMethod || "GET";
  10. let url = httpUrl || "";
  11. const options = {
  12. url: url || "",
  13. method: method || "GET"
  14. };
  15. if (
  16. (httpBody.startsWith("{") && httpBody.endsWith("}")) ||
  17. (httpBody.startsWith("[") && httpBody.endsWith("]"))
  18. ) {
  19. options.body = Body.json(JSON.parse(httpBody));
  20. } else if (httpBody !== '') {
  21. options.body = Body.text(httpBody)
  22. }
  23. client.request(options)
  24. .then(onMessage)
  25. .catch(onMessage);
  26. }
  27. </script>
  28. <form on:submit|preventDefault={makeHttpRequest}>
  29. <select class="button" id="request-method" bind:value={httpMethod}>
  30. <option value="GET">GET</option>
  31. <option value="POST">POST</option>
  32. <option value="PUT">PUT</option>
  33. <option value="PATCH">PATCH</option>
  34. <option value="DELETE">DELETE</option>
  35. </select>
  36. <input id="request-url" placeholder="Type the request URL..." bind:value={httpUrl} />
  37. <br />
  38. <textarea id="request-body" placeholder="Request body" rows="5" style="width:100%;margin-right:10px;font-size:12px"
  39. bind:value={httpBody} />
  40. <button class="button" id="make-request">
  41. Make request
  42. </button>
  43. </form>