btpoole
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017
06:24 PM
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.
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.
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.
9 REPLIES 9
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017
10:08 PM
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.
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.
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017
10:29 PM
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
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2017
10:36 PM
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);
$input=@file_get_contents("php://input");
$event_json=json_decode($input,true);
btpoole
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
07:26 AM
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.
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
12:18 PM
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.
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.
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
12:25 PM
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.
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
12:47 PM
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.
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.
destruk
Streaming Star
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
03:32 PM
Re: AsyncPostFromString ??
No problem renojim!
I was talking about the code in the thread post above where he was using a url of
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.
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.
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2017
09:02 PM
Re: AsyncPostFromString ??
Ah, I see! I hadn't noticed that. Got to agree with you there!
-JT
-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.
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.