Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
DavidS1
Channel Surfer

GetToString() occasionally returns nothing

Jump to solution

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.

0 Kudos
1 Solution

Accepted Solutions
renojim
Community Streaming Expert

Re: GetToString() occasionally returns nothing

Jump to solution

Before you  use GetString() you should be checking the response code with rsp.GetResponseCode().  If it's not 200 reissue the async transfer.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.

View solution in original post

2 REPLIES 2
renojim
Community Streaming Expert

Re: GetToString() occasionally returns nothing

Jump to solution

Before you  use GetString() you should be checking the response code with rsp.GetResponseCode().  If it's not 200 reissue the async transfer.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
DavidS1
Channel Surfer

Re: GetToString() occasionally returns nothing

Jump to solution

Hey, I made a recursive function to do that and it seems to work fine. Here is the code I used : 

 

'*****************************************************************************************
' GetContent
'*****************************************************************************************
sub GetContent()
    ' request the content feed from the API
    xfer = CreateObject("roURLTransfer")
    xfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
    xfer.InitClientCertificates()
    port = CreateObject("roMessagePort")
    xfer.SetMessagePort(port)

    xfer.SetURL("https://myvalidurl.com/page?parameter=sample")

    data = TryToGetData(xfer, port, 5)
    if data <> invalid and data <> ""
        ProcessContent(data)
    else
        m.top.error++
    end if

end sub

'*****************************************************************************************
' TryToGetData
'*****************************************************************************************
function TryToGetData(xfer, port, numberOfTries)

    if numberOfTries < 0 
        return invalid
    end if

    if xfer.AsyncGetToString()
        rsp = wait(10000, port)
        print type(rsp)
        if type(rsp) = "roUrlEvent"
            
            code = rsp.GetResponseCode()
            print "Response code is " code

            if code <> 200
                print "Re-trying to load content"
                triesRemaining = numberOfTries - 1
                return TryToGetData(xfer, port, triesRemaining)
            else
                print "Content retrieved correctly"
                return rsp.GetString()
            end if

        end if
    end if
    ' Should never get here
    return invalid
end function
0 Kudos