Support Forums
HTTPSend response do not change(invalid) when the URL change.

TLDR: What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call? The function's url parameter changes from a droplist widget.

Hello,
I have been testing different ways to run the httpsend function. In the script block of ‘main’ module, in a called module, and as a launched module.
Goal is to change the api link according to user preference(monthly or annual), fetch the data and plot it :

657fe56f1ada9

(the script runs on trigger tag every 7 seconds)
When I tried it, the function only works at application startup ( i.e. response struct object returns correct data)
But after changing api URL from monthly to annual, the whole script runs again as usual, on trigger. But a new api call should bring new data; that’s not happening; specifically the httpsend’s pResponse struct is not populating with new data even though the URL argument changes
( from https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**monthly**&.....
to https://api.eia.gov/v2/naturalgas/pri/sum/....&frequency=**annual**&..... ).

The following code works at first run, but when changed from monthly to annual, httpResponse do not get new annual data and hold the monthly data…


Main [

    If PointObj\Value == 1;
    [
    url = "https://api.eia.gov/v2/natural-gas/pri/sum/data?data[]=value&facets[duoarea][]=SLA&facets[process][]=PIN&api_key=N8u2ivHYBhDYWf0WEQ3Wr4GoIQYAMpCZxA9gP8JM&start=2003-01&sort[0][column]=period&sort[0][direction]=asc&frequency=annual";

    \System\HTTPClient\HTTPSend( url, &httpResponse);

    parsedData = JSONParse(httpResponse\Body);

    { …
    code for preparing the dataarray and timearray,
    then using writehistory to a given tag's historian. Then         
    plotting. 
    …
    }

    ScriptObj\value = CurrentTime();
]

]

Running this as a submodule:
According to docsLaunched Module is fit for this function

When running the HTTPSend function in a launched module, the debugger shows this:

657fe79ce1324

Look like the whole script runs, the module runs seperately as a 'launched module'
Httpresponse is always invalid and thus Valid(parsedData) also shows false(0) in tag browser Script tag's value

Another unusual way was Using WhileLoop(!) but the app crashes due to memory overload =>


<
{============================= System\APICall Launched Module ================================}
{ A simple called module to increment and display a value                     }
{=============================================================================}
APICall
(
  parsedDataptr;
)
[
  httpResponse;
]

Main [

    If 1; [
        WhileLoop(
            Valid(httpResponse) == FALSE;
            \System\HTTPClient\HTTPSend( url, &httpResponse);
            *parsedDataptr = JSONParse(httpResponse\Body);
        );
        ScriptObj\value =  Concat("parsedData Valid ? ", Valid(parsedData), " CurrTime: ", CurrentTime());      
        Slay();
    ]
]
>

What is the proper way to ‘wait’ for HTTPSend function to finish its entire api call ?

(Also asking for tips about way to streamline coding and debugging in VTScada, need to restart VT everytime there is a change in code, and source debugger is not the... 'friendliest' tool.)

TLDR: What is the proper way to &lsquo;wait&rsquo; for HTTPSend function to finish its entire api call? The function&#039;s url parameter changes from a droplist widget. Hello, I have been testing different ways to run the httpsend function. In the script block of &lsquo;main&rsquo; module, in a called module, and as a launched module. **Goal** is to change the api link according to user preference(monthly or annual), fetch the data and plot it : ![657fe56f1ada9](serve/attachment&amp;path=657fe56f1ada9) (the script runs on trigger tag every 7 seconds) When I tried it, the function only works at application startup ( i.e. response struct object returns correct data) But after changing api URL from monthly to annual, the whole script runs again as usual, on trigger. But a new api call should bring new data; that&rsquo;s not happening; specifically the httpsend&rsquo;s pResponse struct is not populating with new data even though the URL argument changes ( from https://api.eia.gov/v2/naturalgas/pri/sum/....&amp;frequency=**monthly**&amp;..... to https://api.eia.gov/v2/naturalgas/pri/sum/....&amp;frequency=**annual**&amp;..... ). The following code works at first run, but when changed from monthly to annual, httpResponse do not get new annual data and hold the monthly data&hellip; ```` Main [ If PointObj\Value == 1; [ url = &quot;https://api.eia.gov/v2/natural-gas/pri/sum/data?data[]=value&amp;facets[duoarea][]=SLA&amp;facets[process][]=PIN&amp;api_key=N8u2ivHYBhDYWf0WEQ3Wr4GoIQYAMpCZxA9gP8JM&amp;start=2003-01&amp;sort[0][column]=period&amp;sort[0][direction]=asc&amp;frequency=annual&quot;; \System\HTTPClient\HTTPSend( url, &amp;httpResponse); parsedData = JSONParse(httpResponse\Body); { &hellip; code for preparing the dataarray and timearray, then using writehistory to a given tag&#039;s historian. Then plotting. &hellip; } ScriptObj\value = CurrentTime(); ] ] ```` **Running this as a submodule:** According to docs[Launched Module is fit for this function](https://www.vtscada.com/help/Content/Scripting/API/LaunchedVsCalled.htm?#:~:text=function%20is%20an%20example.) When running the HTTPSend function in a launched module, the debugger shows this: ![657fe79ce1324](serve/attachment&amp;path=657fe79ce1324) Look like the whole script runs, the module runs seperately as a &#039;launched module&#039; Httpresponse is always invalid and thus Valid(parsedData) also shows false(0) in tag browser Script tag&#039;s value Another unusual way was Using WhileLoop(!) but the app crashes due to memory overload =&gt; ```` &lt; {============================= System\APICall Launched Module ================================} { A simple called module to increment and display a value } {=============================================================================} APICall ( parsedDataptr; ) [ httpResponse; ] Main [ If 1; [ WhileLoop( Valid(httpResponse) == FALSE; \System\HTTPClient\HTTPSend( url, &amp;httpResponse); *parsedDataptr = JSONParse(httpResponse\Body); ); ScriptObj\value = Concat(&quot;parsedData Valid ? &quot;, Valid(parsedData), &quot; CurrTime: &quot;, CurrentTime()); Slay(); ] ] &gt; ```` **What is the proper way to &lsquo;wait&rsquo; for HTTPSend function to finish its entire api call ?** (Also asking for tips about way to streamline coding and debugging in VTScada, need to restart VT everytime there is a change in code, and source debugger is not the... &#039;friendliest&#039; tool.)
edited Dec 20 '23 at 9:37 am

Hey @mahin101,

It looks like you have created a nice tool to import data. While I don't have the quick solution to your issue, I do have a link to a video which will hopefully make the debugging process a bit easier for you:

Video on Advanced Diagnostics: https://www.youtube.com/watch?v=WizPfwUrGlQ

Hey @mahin101, It looks like you have created a nice tool to import data. While I don&#039;t have the quick solution to your issue, I do have a link to a video which will hopefully make the debugging process a bit easier for you: Video on Advanced Diagnostics: https://www.youtube.com/watch?v=WizPfwUrGlQ

Trihedral Engineering Ltd.

I would try creating a custom tag type and embed the code in there, that will solve your restarting issue.

Make another state that you call when Valid(&parsedData)
Cause of the steady state you can't have the call and the ScriptObject\Value set in the same state cause you can't guarantee execution order.

And remove "ScriptObj\value =" from the launched module.

I would try creating a custom tag type and embed the code in there, that will solve your restarting issue. Make another state that you call when Valid(&amp;parsedData) Cause of the steady state you can&#039;t have the call and the ScriptObject\Value set in the same state cause you can&#039;t guarantee execution order. And remove &quot;ScriptObj\value =&quot; from the launched module.
edited Jan 12 at 4:14 am
101
2
3
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft