http.js 949 B

123456789101112131415161718192021222324252627282930313233
  1. const methodSelect = document.getElementById("request-method");
  2. const requestUrlInput = document.getElementById("request-url");
  3. const requestBodyInput = document.getElementById("request-body");
  4. let client
  5. window.__TAURI__.http.getClient().then(function (c) {
  6. client = c
  7. })
  8. document.getElementById("make-request").addEventListener("click", function () {
  9. const method = methodSelect.value || "GET";
  10. const url = requestUrlInput.value || "";
  11. const options = {
  12. url: url,
  13. method: method,
  14. };
  15. let httpBody = requestBodyInput.value || "";
  16. if (
  17. (httpBody.startsWith("{") && httpBody.endsWith("}")) ||
  18. (httpBody.startsWith("[") && httpBody.endsWith("]"))
  19. ) {
  20. options.body = window.__TAURI__.http.Body.json(JSON.parse(httpBody));
  21. } else if (httpBody !== '') {
  22. options.body = window.__TAURI__.http.Body.text(httpBody)
  23. }
  24. client
  25. .request(options)
  26. .then(registerResponse)
  27. .catch(registerResponse);
  28. });