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: 
btpoole
Channel Surfer

AsyncPostFromString ??

Nor really sure if I am doing this right or not, must not be since it's not working. I am sending a request to my server to save the info. Each piece of info is an array that is formatjson and escaped.

ut = CreateObject("roURLTransfer")
ut.SetPort(CreateObject("roMessagePort"))
myname = FormatJson(name)
myposter= FormatJson(hdposterUrl)
mystream= FormatJson(stream)
mygid= FormatJson(gid)
mynameJsonEscaped = ut.Escape(myname)
myposterJsonEscaped= ut.Escape(myposter)
mystreamJsonEscaped= ut.Escape(mystream)
mygidJsonEscaped= ut.Escape(mygid)

url="http://xxx.xxx.x.xxx:8000/Favorite/myCreate.php?filename="+myfile + "&channel=" + mynameJsonEscaped + "&hdposterUrl=" + myposterJsonEscaped + "&stream=" + mystreamJsonEscaped + "&gid=" + mygidJsonEscaped

If ut.AsyncPostFromString(url) Then
    event=wait(timeout, ut.GetPort())
      if type(event)= "roUrlEvent"
      print  event.GetResponseCode()
      if event.GetResponseCode()= 200
      result=event.PostFromString(url)
      ?result
        m.urlCode=event.GetResponseCode()
        m.failure=event.GetFailureReason()
        return m.urlCode
        else
        m.urlCode=event.GetResponseCode()
        ?"FAILURE CODE "m.urlCode
        return result
        end if
      elseif event = invalid
  ut.AsyncCancel()
          endif
      end if

I can't seem to get it to work. I can copy the completed url from debugger and past into browser with approx 10 elements in each array and it works. Anything more it doesn't work or crashes browser. When ran on roku I get a -3 back. Any guidance appreciated.
0 Kudos
9 REPLIES 9
destruk
Streaming Star

Re: AsyncPostFromString ??

Your url you are using is acting as a GET request, not a POST request.
With a GET request the data goes appended to the url string like you have it.
With a POST request the data is sent in the request BODY itself - the url is just the basic url without any variables strung along in the url.


For aSyncPostFromString you would use
ut.seturl("http://xxx.xxx.x.xxx:8000/Favorite/myCreate.php")
ut.asyncpostfromstring(data)

To keep things simple, you should probably format your data as json LAST.
Try merging all your available data you want to send into a single array, then format that new array and you have a single variable string as the result.
ie
maindata=[]
input={}
input["name"]=name
input["hdposterurl"]=hdposterurl
input["stream"]=stream
input["gid"]=gid
maindata.push(input)
data=formatjson(maindata)

alternately you can build the data string manually - 
data="[{name:"+chr(34)+name+chr(34)+",hdposterurl:"+chr(34)+hdposterurl+chr(34)+",stream:"+chr(34)+stream+chr(34)+",gid:"+chr(34)+gid+chr(34)+"}]"
Something like that should work better.

If you are using scenegraph and this is in a task thread, then you shouldn't even need to do it asynchronously - I'm pretty sure it could block for all roku cares as a subsequent call to the task node will simply create a new instance of the task node.
0 Kudos
destruk
Streaming Star

Re: AsyncPostFromString ??

As an added value benefit - when using json if you aren't using these characters within your strings then you don't need to escape anything -

  • Backspace

  • Form feed

  • Newline

  • Carriage return

  • Tab

  • Double quote

  • Backslash
0 Kudos
destruk
Streaming Star

Re: AsyncPostFromString ??

As your get request works, and the post request doesn't, for your server-side script you'll want to decode the json being sent through the POST method -
$input=@file_get_contents("php://input");
$event_json=json_decode($input,true);
0 Kudos
btpoole
Channel Surfer

Re: AsyncPostFromString ??

"destruk" wrote:
As your get request works, and the post request doesn't, for your server-side script you'll want to decode the json being sent through the POST method -
$input=@file_get_contents("php://input");
$event_json=json_decode($input,true);

destruk, thank you for explaining as well as showing examples. I understand now. Really not up on php much but do understand GET and POST better with your help. Worked great. Thanks again.
0 Kudos
renojim
Community Streaming Expert

Re: AsyncPostFromString ??

"destruk" wrote:
Your url you are using is acting as a GET request, not a POST request.
With a GET request the data goes appended to the url string like you have it.
With a POST request the data is sent in the request BODY itself - the url is just the basic url without any variables strung along in the url.

You can do POST requests, async or otherwise, with variables appended to the URL; I do it all the time.  The confusing part is that in PHP you access those variables with $_GET.  There's a thread around here someplace that discusses this.

-JT
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.
0 Kudos
destruk
Streaming Star

Re: AsyncPostFromString ??

Just because you can doesn't mean you should -- if a get request string url is too long your server will lose data on the request.  Apache will experience problems if your url length is longer than 4000 characters.  Post data is limited to 8 megabytes by default.  I just find it easier to code for without mixing and matching request types.  Appending get variables to a post means you need to handle both on the other end - and definitely doubling them up sending the same variables both ways in the same request is inefficient.
0 Kudos
renojim
Community Streaming Expert

Re: AsyncPostFromString ??

"destruk" wrote:
Just because you can doesn't mean you should -- if a get request string url is too long your server will lose data on the request.  Apache will experience problems if your url length is longer than 4000 characters. Post data is limited to 8 megabytes by default.

Well obviously I'm not suggesting you use this method to post 8MB worth of data.
 
"destruk" wrote:
I just find it easier to code for without mixing and matching request types.  Appending get variables to a post means you need to handle both on the other end - and definitely doubling them up sending the same variables both ways in the same request is inefficient.

You lost me here.  Doubling them up?  Doubling what up?  I'll have to dig up that old thread.  The tldr version is that there's literally no difference between using GET and POST with URL variables when using PHP to handle the request.  None, zip, nothing.  You can use identical PHP code and just change your GETs to POSTs or vice versa.  As was brought up in that thread by EnTerr, a GET could be cached whereas a POST won't be, so if you're using GET to report something back to your server it could get "lost" along the way.

-JT
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.
0 Kudos
destruk
Streaming Star

Re: AsyncPostFromString ??

No problem renojim!
I was talking about the code in the thread post above where he was using a url of 
url="http://xxx.xxx.x.xxx:8000/Favorite/myCreate.php?filename="+myfile + "&channel=" + mynameJsonEscaped + "&hdposterUrl=" + myposterJsonEscaped + "&stream=" + mystreamJsonEscaped + "&gid=" + mygidJsonEscaped


Followed by aSyncPostfromString(url) - which would send doubled data - the full url as the post as well as the variables as a url as a get.  There isn't any good reason I can think of for sending the same variables both ways.
0 Kudos
renojim
Community Streaming Expert

Re: AsyncPostFromString ??

Ah, I see! I hadn't noticed that. Got to agree with you there!

-JT
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.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.