Search code examples
rokubrightscript

Difference between AsyncGetToString and AsyncPostFromString?


I create one web API. and I call in bright-script. I refer https://developer.roku.com/en-gb/docs/references/brightscript/interfaces/ifurltransfer.md#head-as-dynamic/ all the method but don't understand anyone knows its real use for AsyncGetToString and AsyncPostFromString method.

I use the following code in Roku

readInternet = createObject("roUrlTransfer")
      readInternet.setUrl(url)
      readInternet.setport(m.port)

      readInternet.gettostring()
      timer = createobject("roTimeSpan")
      timer.Mark()
      readInternet.AsyncPostFromString() 'readInternet.AsyncGetToString

But its every time fire Get method in My Roku Server.

here the Roku server code(using Get method)

public string Get(int id)
{
            return "The vlaue is: " + id;
}

its always call this method both the way (using Post method)

 [HttpPost] // OWIN - Open Web Interface for .NET
 public HttpResponseMessage Post([FromUri]string name, [FromUri]string pass) // Its use both FromBody (complex type from the query string) and FromUri(primitive type from the request body)
 {
      //return "UserName Details :" + name + pass;
      return Request.CreateResponse(HttpStatusCode.OK, name + " " + pass); //Using Post Method
 }

please any one help me.


Solution

  • AsyncPostFromString() allows you to make an asynchronous POST request which once completed will send a message to the message port associated with it (in this case m.port).

    m.port = createObject("roMessagePort")
    readInternet = createObject("roUrlTransfer")
    readInternet.setUrl(url)
    readInternet.setMessagePort(m.port)
    if readInternet.asyncPostFromString("your_post_data_string") then
        msg = m.port.waitMessage(0)
        if type(msg) = "roUrlEvent" then
            print msg
        end if
    end if
    

    This should make the correct POST request to your server's endpoint. Note that you need to pass the POST data as a parameter in asyncPostFromString()