Page 1 of 1

Getting the contents of a web request using load url

Posted: Sun Oct 27, 2024 1:34 pm
by andyh1234
Hi,

Im looking to use load url instead of get to save long pauses in my app.

I have the code

Code: Select all

on validateAPI
  put "https://www.sample.com/v1/accounts/" into tURL
  load url tUrl with message "validationLoaded"
end validateAPI

on validationLoaded pURL, pURLStatus
   if pURLStatus is "cached" then
      ## LiveCode has cached URL content. Accessing the url uses
      ## the cache. 
      answer  "Result" && pURL
      		
      ## Remove URL from cache if you are all done with it.
      unload url pURL
   else
      put libURLErrorData(pURL) into tError
     answer "Error" && tError
   end if
   
end validationLoaded
what do I have to add to get the page data? I can see the URL that has been called and its status (cached or error) fine, but I don't know what to do to get the actual page data and cant find any examples anywhere.

Thanks

Re: Getting the contents of a web request using load url

Posted: Tue Jan 14, 2025 8:16 am
by helenecrane
I think you should you need to access the content of the URL within your validationLoaded handler. You can do this by using the libURLData function, which retrieves the data returned from the URL once it has been loaded successfully.

Here’s how you can modify your validationLoaded handler to get the page data:

Code: Select all

livecode

on validateAPI
  put "https:// www. sample. com/v1/accounts/" into tURL
  load url tURL with message "validationLoaded"
end validateAPI

on validationLoaded pURL, pURLStatus
   if pURLStatus is "cached" or pURLStatus is "success" then
      ## LiveCode has successfully loaded the URL content.
      put libURLData(pURL) into tData
      answer "Result: " && tData
      		
      ## Remove URL from cache if you are all done with it.
      unload url pURL
   else
      put libURLErrorData(pURL) into tError
      answer "Error: " && tError
   end if
end validationLoaded