2

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.

1 Answers1

2

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()

juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • Thank you thenaz I try to the following code URL pass in "readInternet.setUrl" and parameter pass in "asyncPostFromString("here")". It's working properly. Thank you again. – Nikunj Chaklasiya Jun 17 '19 at 04:36
  • What is sent in `asyncPostFromString()` is the body of the POST request. If you want to pass data as a query string simply append it to the URL. – juliomalves Jun 22 '19 at 13:15
  • I used `readInternet.setUrl("http://localhost:1579/api/DefaultAPI/") readInternet.SetRequest("POST") readInternet.asyncPostFromString("?name=namevariable&pass=passvariable")` here not append. But i pass directly string here `http://localhost:1579/api/DefaultAPI/?name=namevariable&pass=passvariable` its working perfectly. – Nikunj Chaklasiya Jun 24 '19 at 04:03