haamro
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
09:21 AM
AsyncPostFromSTring / GetString only getting 2 Character
Hello All, I am anewbie, in a process of making my first roku app which fetches data from another website after Logging in. I have attached all the necessary headers and using AsyncPostFromSTring to post the data. I am getting a msg.GetResponseHeadersr but with msg.GetString() it only revelas two character "< []" (small box).
I don't know what I am doing wrong. GetResponseHeader says "transfer-encoding: chunked" , is this where the issue is ?
this is my code
Any help is appreciated. Thank you
I don't know what I am doing wrong. GetResponseHeader says "transfer-encoding: chunked" , is this where the issue is ?
this is my code
loginURL="http://www.example.com/members/logmein.php"
username="uname"
password="pword"
postContent="username="+username+"&password="+password
request = CreateObject("roURLTransfer")
xport=CreateObject("RoMessagePort")
request.InitClientCertificates()
request.SetCertificatesFile("common:/certs/ca-bundle.crt")
request.setUrl(loginURL)
request.SetMessagePort(xport)
request.RetainBodyOnError(true)
REM **** THIS headers works fine in PHP Curl ****
request.AddHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
request.AddHeader("Accept-Encoding","gzip, deflate")
request.AddHeader("Accept-Language","en-US,en;q=0.8,hi;q=0.6,ne;q=0.4")
request.AddHeader("Cache-Control","max-age=0")
request.AddHeader("Connection","keep-alive")
request.AddHeader("Content-Type","application/x-www-form-urlencoded")
request.AddHeader("Host","www.example.com")
request.AddHeader("Origin","http://www.example.com")
request.AddHeader("Referer","http://www.example.com/logmein.php")
request.AddHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36")
request.EnableCookies()
if (request.asyncPostFromSTring(postContent))
msg = wait(0,xport)
if type(msg) = "roUrlEvent"
Print "we are getting some input"
Print msg.GetResponseHeaders() ' This prints header
print msg.GetString() ' this doesn't work
end if
end if
Any help is appreciated. Thank you
8 REPLIES 8

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
09:43 AM
Re: AsyncPostFromSTring / GetString only getting 2 Character
My first guess is your document contains non-ASCII characters (maybe a null?) that cause the print to fail. Try printing msg.GetString().Len() and see if it really contains two characters or if there are more that aren't getting printed.
--Mark
--Mark
haamro
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
09:59 AM
Re: AsyncPostFromSTring / GetString only getting 2 Character
Thanx for the prompt reply,
msg.GetString().Len() => 2 . So it is indeed two character
I have a similar script written in PHP, which gets the entire content. So I know all headers/login process/request I am making are valid. The only thing I can think of is roku not handling "transfer-encoding: chunked".
msg.GetString().Len() => 2 . So it is indeed two character
I have a similar script written in PHP, which gets the entire content. So I know all headers/login process/request I am making are valid. The only thing I can think of is roku not handling "transfer-encoding: chunked".
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
11:23 AM
Re: AsyncPostFromSTring / GetString only getting 2 Character
At the very least you should check your HTTP response code:
after your roUrlEvent completes.
print "Response Code: "; msg.GetResponseCode ()
after your roUrlEvent completes.
haamro
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
12:31 PM
Re: AsyncPostFromSTring / GetString only getting 2 Character
"belltown" wrote:it is 200
At the very least you should check your HTTP response code:print "Response Code: "; msg.GetResponseCode ()
after your roUrlEvent completes.
haamro
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
01:18 PM
Re: AsyncPostFromSTring / GetString only getting 2 Character
looks like roku cannot handle "gzip"
changed to
Its working now 🙂
changed to
request.AddHeader("Accept-Encoding","deflate")
Its working now 🙂
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
01:30 PM
Re: AsyncPostFromSTring / GetString only getting 2 Character
"haamro" wrote:
looks like roku cannot handle "gzip"
It can handle gzip encoding, but you have to tell it to do so:
request.EnableEncodings (True)
haamro
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
06:24 PM
Re: AsyncPostFromSTring / GetString only getting 2 Character
Thanx. Encoding enabled.
I am getting the entire body but I didn't realized that my form with username and password was not posted. It didn't log me in. Any thoughts ? Also, is there any examples on working with Cookies ?
Once logged in, I need a session / Cookie to fetch multiple files. I couldn't find proper way of dealing with cookies. If the Cookies are enabled, do I need to call them while fetching the next file ?
I am getting the entire body but I didn't realized that my form with username and password was not posted. It didn't log me in. Any thoughts ? Also, is there any examples on working with Cookies ?
Once logged in, I need a session / Cookie to fetch multiple files. I couldn't find proper way of dealing with cookies. If the Cookies are enabled, do I need to call them while fetching the next file ?
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2015
07:01 PM
Re: AsyncPostFromSTring / GetString only getting 2 Character
I'm not quite sure why your form wasn't posted.
One thing to note, however, is that you should be Url-Encoding your form fields if you're submitting it with a Content Type of "application/x-www-form-urlencoded":
Also, you have a lot of AddHeader statements, not all of which are necessary, as the Roku will provide many of the headers itself. I do know that it's safe to change the User-Agent header, but I'd try leaving out the others unless you find a compelling reason to include any of them.
As far as the cookies are concerned, in most of the work I did with cookies I handled them manually. I didn't use EnableCookies, etc. because I wanted my code to run on the older 3.1 firmware. I would assume though that if you use EnableCookies then the "cookie cache" should be able to handle sending the Cookies back to the server, although I can't confirm that.
Another issue with logging in with a username and password is the authentication mechanism used by the server. I've not come across many servers that send plain text usernames/passwords over an unencrypted Http connection. Even many servers that use encrypted Http will use some form of authentication (Basic, Digest, etc.) You should make sure that you are using the same form of authentication that your server requires.
One thing to note, however, is that you should be Url-Encoding your form fields if you're submitting it with a Content Type of "application/x-www-form-urlencoded":
ut = CreateObject ("roUrlTransfer")
postContent = "username=" + ut.Escape (username) + "&password= " + ut.Escape (password)
Also, you have a lot of AddHeader statements, not all of which are necessary, as the Roku will provide many of the headers itself. I do know that it's safe to change the User-Agent header, but I'd try leaving out the others unless you find a compelling reason to include any of them.
As far as the cookies are concerned, in most of the work I did with cookies I handled them manually. I didn't use EnableCookies, etc. because I wanted my code to run on the older 3.1 firmware. I would assume though that if you use EnableCookies then the "cookie cache" should be able to handle sending the Cookies back to the server, although I can't confirm that.
Another issue with logging in with a username and password is the authentication mechanism used by the server. I've not come across many servers that send plain text usernames/passwords over an unencrypted Http connection. Even many servers that use encrypted Http will use some form of authentication (Basic, Digest, etc.) You should make sure that you are using the same form of authentication that your server requires.