Search code examples
string-formattingrokubrightscript

URL Encoding a variable for a web service call on Roku


I need to URL encode a variable that is being passed into a web service.

testcategory = "Action and Adventure"
jsonRequest = CreateObject("roUrlTransfer")
jsonRequest.SetURL("http://myurl.com/?method=getRokuCategorizedSeriesListing&category=" + testcategory)

I need to have whatever the value be for "testcategory" to be url encoded to be passed it to this web service call. In this example I would need "Action and Adventure" to be "Action%20and%20Adventure"

Is there a Brightscript function to accomplish this?

Any help would be very appreciated.

Thank you!


Solution

  • Place this function in one of your brightscript file

    Function HttpEncode(str As String) As String
        o = CreateObject("roUrlTransfer")
        return o.Escape(str)
    End Function
    

    and then you can use the function HttpEncode as

    jsonRequest.SetURL("http://myurl.com/?method=getRokuCategorizedSeriesListing&category=" + HttpEncode(testcategory) )