GetToString() occasionally returns nothing
Hello! I have been using the GetToString() function to retrieve my content, but it seems that it fails one every 10 times.
Here is the code I am using
' request the content feed from the API xfer = CreateObject("roURLTransfer") xfer.SetCertificatesFile("common:/certs/ca-bundle.crt") xfer.InitClientCertificates() url = "https://myvalidurl.com/page?parameter=sample" xfer.SetURL(url) rsp = xfer.GetToString()
I am unsure why but rsp is equal to an empty string once every 10 times and the correct content the rest of the time.
I am 100% sure that my URL link is valid when the code fails to retrieve the content for that URL is static and never changes. On a web browser, the content is always returned with a 100% success rate.
I tried using an async version of the same code but it yielded the same results :
'request the content feed from the API xfer = CreateObject("roURLTransfer") xfer.SetCertificatesFile("common:/certs/ca-bundle.crt") xfer.InitClientCertificates() url = "https://myvalidurl.com/page?parameter=sample" port = CreateObject("roMessagePort") xfer.SetMessagePort(port) print url xfer.SetURL(url) if xfer.AsyncGetToString() rsp = wait(10000, port) print type(rsp) if type(rsp) = "roUrlEvent" rawData = rsp.GetString() ProcessContent(rawData) end if else 'Should not get there notifyError = true end if
What happens on that bit of code is rawData is indeed populated but with an empty string ("") instead of the correct data. My code works 90% of the time but sometimes the content is just failed to be retrieved.
Any idea of what I should do to secure the content retrieval? I could re-run the URL request if it returns empty but it does not seem like a pretty solution.
Thank you for your help.
Before you use GetString() you should be checking the response code with rsp.GetResponseCode(). If it's not 200 reissue the async transfer.