Recent

Author Topic: http post and get requests  (Read 2498 times)

prof7bit

  • Full Member
  • ***
  • Posts: 144
http post and get requests
« on: October 25, 2021, 03:02:09 pm »
Because nobody dared to ask before I will do it now: I need to be able to do GET and POST requests.

I have found an example using await() and window.fetch() and this works for issuing GET requests, but now I also need something for POST requests and I can't find any example.

There is also a TJSXMLHttpRequest class but I cannot even figure out how to create an instance of it because of its obscure constructor arguments.

Are there any examples of POST and GET using TJSXMLHttpRequest? Or even better an example of using POST with window.fetch(), or still better some convenient pascal-ish wrapper API that hides away all these javascrip-ish procedures and just gives me a simple function to call?



PascalDragon

  • Hero Member
  • *****
  • Posts: 3490
  • Compiler Developer
Re: http post and get requests
« Reply #1 on: October 26, 2021, 09:08:47 am »
This is based on some code I have (I've not tested it as-is):

Code: Pascal  [Select][+][-]
  1. procedure PostData(aData: String);
  2. var
  3.   xhr: TJSXMLHttpRequest;
  4.  
  5.   procedure AfterLoad(aEvent: TEventListenerEvent);
  6.   begin
  7.     if xhr.Status = 200 then
  8.       Writeln('Ok')
  9.     else
  10.       Writeln('Error: ', xhr.Status);
  11.   end;
  12.  
  13. begin
  14.   xhr := TJSXMLHttpRequest.new;
  15.   xhr.addEventListener('load', @AfterLoad);
  16.   xhr.open('POST', '/myapp/myapi');
  17.   xhr.send(aData);
  18. end;
  19.  

Please note that the AfterLoad will be executed asynchronously and that PostData will return right after the send while the request is still being executed.

dbannon

  • Hero Member
  • *****
  • Posts: 1674
    • tomboy-ng, a rewrite of the classic Tomboy
Re: http post and get requests
« Reply #2 on: October 26, 2021, 12:00:26 pm »
https://github.com/tomboy-notes/tomboy-ng/blob/master/source/transgithub.pas

line 1329 has a function that does both PUT and POST.

line 1276 does a GET

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

prof7bit

  • Full Member
  • ***
  • Posts: 144
Re: http post and get requests
« Reply #3 on: October 26, 2021, 12:46:41 pm »
This is based on some code I have (I've not tested it as-is):

Code: Pascal  [Select][+][-]
  1. procedure PostData(aData: String);
  2. var
  3.   xhr: TJSXMLHttpRequest;
  4.  
  5.   procedure AfterLoad(aEvent: TEventListenerEvent);
  6.   begin
  7.     if xhr.Status = 200 then
  8.       Writeln('Ok')
  9.     else
  10.       Writeln('Error: ', xhr.Status);
  11.   end;
  12.  
  13. begin
  14.   xhr := TJSXMLHttpRequest.new;
  15.   xhr.addEventListener('load', @AfterLoad);
  16.   xhr.open('POST', '/myapp/myapi');
  17.   xhr.send(aData);
  18. end;
  19.  

Please note that the AfterLoad will be executed asynchronously and that PostData will return right after the send while the request is still being executed.

Thank you!

But I wonder: How does it keep the local stack frame alive to access the local variable when the function returns immediately? Is the nested function translated into a closure that will keep a reference to the XHR variable (which would then not live on the stack at all)?

What other things are allowed here that would normally not work at all, like for example returning pointers to local variables, etc.? Are these things (and their limits) clearly defined as new features or do they just happen under certain circumstances (like here) as a side effect and nobody is talking about it?

PascalDragon

  • Hero Member
  • *****
  • Posts: 3490
  • Compiler Developer
Re: http post and get requests
« Reply #4 on: October 27, 2021, 09:21:07 am »
But I wonder: How does it keep the local stack frame alive to access the local variable when the function returns immediately? Is the nested function translated into a closure that will keep a reference to the XHR variable (which would then not live on the stack at all)?

The JavaScript engine is dealing with all the life cycle management of the variables, but yes, they're essentially closures. Anonymous functions are supported by pas2js as well, so you can write this, too:

Code: Pascal  [Select][+][-]
  1. procedure PostData(aData: String);
  2. var
  3.   xhr: TJSXMLHttpRequest;
  4. begin
  5.   xhr := TJSXMLHttpRequest.new;
  6.   xhr.addEventListener('load', procedure(aEvent: TEventListenerEvent)
  7.     begin
  8.       if xhr.Status = 200 then
  9.         Writeln('Ok')
  10.       else
  11.         Writeln('Error: ', xhr.Status);
  12.     end);
  13.   xhr.open('POST', '/myapp/myapi');
  14.   xhr.send(aData);
  15. end;

What other things are allowed here that would normally not work at all, like for example returning pointers to local variables, etc.? Are these things (and their limits) clearly defined as new features or do they just happen under certain circumstances (like here) as a side effect and nobody is talking about it?

You can't have pointers in JavaScript and thus pas2js (at least not in the way you're thinking). Passing on references to classes, etc. is safe however, cause again, their life cycle is managed by the JS engine.

 

TinyPortal © 2005-2018