www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibe.d http client

reply Lars Johansson <lasse 11dim.se> writes:
hi all,
this is driving me nuts.

My client code:
I call the server once for each node in the array endpoints.
but the result (see below) is three calls with the last element 
(/times)
i have tried a zillion times in vain, they all ends with three 
(/times) call.
the only way i can get this right is by break out the calling 
logic to a function like
void spawnRequest(string ep){...} and thus creating a copy per 
call I suppose.
Two things:
It works but, it is a clumsy solution.
And I'm not really sure why it goes wrong.
i would appreciate if someone can show me a clean solution 
without a 'technical' function.  and why my solution goes wrong


import vibe.vibe;
import std.stdio : writefln;
import std.stdio : writeln;

     void main(){
         string[] endpoints = ["/", "/hello/VibeClient", "/time"];
         foreach (endpoint; endpoints) {
     		writeln("start ",endpoint);
             auto ep = endpoint;
             runTask(() nothrow  safe {
                 try {
                     auto response = 
requestHTTP("http://127.0.0.1:8080" ~ ep);
                     auto body = response.bodyReader.readAllUTF8();
                     ()  trusted {
                         writefln("[%s] Response: %s", ep, body);
                     }();
                 }
                 catch (Exception e) {
                     try
                     {
                         ()  trusted {
                             writefln("[%s] Error: %s", ep, e.msg);
                         }();
                     }
                     catch (Exception) {}
                 }
             });
         }
         runApplication();
     }

result:
PS C:\Users\Lasse\DUB\nwclient01> ./nwclient01.exe
start /
start /hello/VibeClient
start /time
[/time] Response: Current time: 2026-02-02T17:28:07.6319941
[/time] Response: Current time: 2026-02-02T17:28:07.632999
[/time] Response: Current time: 2026-02-02T17:28:07.632999
Feb 02
next sibling parent Sergey <kornburn yandex.ru> writes:
On Monday, 2 February 2026 at 16:47:52 UTC, Lars Johansson wrote:
 hi all,
 this is driving me nuts.
Haven't checked this precisely, but probably it relates to one very old bug in D of how closures are handled.
Feb 02
prev sibling parent reply Arjan <arjan ask.me.to> writes:
On Monday, 2 February 2026 at 16:47:52 UTC, Lars Johansson wrote:
 hi all,
 this is driving me nuts.

 My client code:
 I call the server once for each node in the array endpoints.
 but the result (see below) is three calls with the last element 
 (/times)
 i have tried a zillion times in vain, they all ends with three 
 (/times) call.
 the only way i can get this right is by break out the calling 
 logic to a function like
 void spawnRequest(string ep){...} and thus creating a copy per 
 call I suppose.
 Two things:
 It works but, it is a clumsy solution.
 And I'm not really sure why it goes wrong.
 i would appreciate if someone can show me a clean solution 
 without a 'technical' function.  and why my solution goes wrong


 import vibe.vibe;
 import std.stdio : writefln;
 import std.stdio : writeln;

     void main(){
         string[] endpoints = ["/", "/hello/VibeClient", 
 "/time"];
         foreach (endpoint; endpoints) {
     		writeln("start ",endpoint);
             auto ep = endpoint;
             runTask(() nothrow  safe {
                 try {
                     auto response = 
 requestHTTP("http://127.0.0.1:8080" ~ ep);
                     auto body = 
 response.bodyReader.readAllUTF8();
                     ()  trusted {
                         writefln("[%s] Response: %s", ep, body);
                     }();
                 }
                 catch (Exception e) {
                     try
                     {
                         ()  trusted {
                             writefln("[%s] Error: %s", ep, 
 e.msg);
                         }();
                     }
                     catch (Exception) {}
                 }
             });
         }
         runApplication();
     }

 result:
 PS C:\Users\Lasse\DUB\nwclient01> ./nwclient01.exe
 start /
 start /hello/VibeClient
 start /time
 [/time] Response: Current time: 2026-02-02T17:28:07.6319941
 [/time] Response: Current time: 2026-02-02T17:28:07.632999
 [/time] Response: Current time: 2026-02-02T17:28:07.632999
Use: ```d runTask(() nothrow safe {...}, ep ); ``` see https://vibed.org/api/vibe.core.core/runTask
Feb 02
parent Lars Johansson <lasse 11dim.se> writes:
On Monday, 2 February 2026 at 17:46:58 UTC, Arjan wrote:
 On Monday, 2 February 2026 at 16:47:52 UTC, Lars Johansson 
 wrote:
 Use:

 ```d
 runTask(() nothrow  safe {...}, ep );
 ```

 see https://vibed.org/api/vibe.core.core/runTask
Hi thanks, it almost worked, the argument {...}, ep ); 'ep' must have another name. I got strange errmsgs with 'ep' i do not understand. Otherwise it is a nice solution. the pgm hangs in the eventloop, but that is something i fixed. It also seems i have to use safe and nothrow all over in my small experiment. I like neither. Safe memory is not a big deal, keep track of your bits. anyway, without your help I would not have solved my problem, thanks again. p.s. I do not undertand the doc on runtask.
Feb 02