Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
haamro
Level 7

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

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
0 Kudos
8 Replies
RokuMarkn
Level 7

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
0 Kudos
haamro
Level 7

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".
0 Kudos
belltown
Level 7

Re: AsyncPostFromSTring / GetString only getting 2 Character

At the very least you should check your HTTP response code:

print "Response Code: "; msg.GetResponseCode ()


after your roUrlEvent completes.
https://github.com/belltown/
0 Kudos
Highlighted
haamro
Level 7

Re: AsyncPostFromSTring / GetString only getting 2 Character

"belltown" wrote:
At the very least you should check your HTTP response code:

print "Response Code: "; msg.GetResponseCode ()


after your roUrlEvent completes.
it is 200
0 Kudos
haamro
Level 7

Re: AsyncPostFromSTring / GetString only getting 2 Character

looks like roku cannot handle "gzip"

changed to

request.AddHeader("Accept-Encoding","deflate")

Its working now Smiley Happy
0 Kudos
belltown
Level 7

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)
https://github.com/belltown/
0 Kudos
haamro
Level 7

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 ?
0 Kudos
belltown
Level 7

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":


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.
https://github.com/belltown/
0 Kudos