Http.svelte 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <script>
  2. import { getClient, Body } from "@tauri-apps/api/http";
  3. let httpMethod = "GET";
  4. let httpUrl = "https://jsonplaceholder.typicode.com/todos/1";
  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).then(onMessage).catch(onMessage);
  24. }
  25. </script>
  26. <form on:submit|preventDefault={makeHttpRequest}>
  27. <select class="button" id="request-method" bind:value={httpMethod}>
  28. <option value="GET">GET</option>
  29. <option value="POST">POST</option>
  30. <option value="PUT">PUT</option>
  31. <option value="PATCH">PATCH</option>
  32. <option value="DELETE">DELETE</option>
  33. </select>
  34. <input
  35. id="request-url"
  36. placeholder="Type the request URL..."
  37. bind:value={httpUrl}
  38. />
  39. <br />
  40. <textarea
  41. id="request-body"
  42. placeholder="Request body"
  43. rows="5"
  44. style="width:100%;margin-right:10px;font-size:12px"
  45. bind:value={httpBody}
  46. />
  47. <button class="button" id="make-request"> Make request </button>
  48. </form>