Http.svelte 1.3 KB

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