wasmhttpdemo.pp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly HTTP API - demo program
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. library wasmhttpdemo;
  12. uses basenenc, sysutils, classes, wasm.http.api, wasm.http.shared, wasm.http.objects;
  13. Procedure HandleResponseCallback(Resp : TWasmHTTPResponse);
  14. var
  15. H : String;
  16. begin
  17. Writeln('Got response on request ID: ',Resp.RequestID);
  18. Writeln('Status: ',Resp.Status,' ',Resp.StatusText);
  19. Writeln('Headers (',Resp.Headers.Count,'):');
  20. For H in Resp.Headers do
  21. Writeln(H);
  22. if Pos('text/',Trim(Resp.Headers.Values['content-type']))=1 then
  23. begin
  24. Writeln('Body is text (Assumed UTF8):');
  25. Writeln(Resp.BodyAsUTF8);
  26. end
  27. else
  28. begin
  29. Writeln('Body is not text, base64 content:');
  30. Writeln(Base64.Encode(Resp.Body));
  31. end;
  32. Writeln('')
  33. end;
  34. procedure StartTest;
  35. Var
  36. Req : TWasmHTTPRequest;
  37. ID : TWasmHTTPRequestID;
  38. begin
  39. Writeln('Creating request');
  40. Req:=TWasmHTTPRequest.Create('index.html');
  41. Writeln('Executing request');
  42. ID:=Req.Execute(@HandleResponseCallback);
  43. Writeln('Got request ID :',ID);
  44. // Request is freed once the return was processed.
  45. end;
  46. var
  47. Buf : Array[1..64*1024] of byte;
  48. begin
  49. SetTextBuf(output,buf,SizeOf(Buf));
  50. StartTest;
  51. end.