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: 
squirreltown
Roku Guru

http POST request

twitbod = http.GetParams("bodyParams").encode()
http.PostFromStringWithTimeout( twitbod, 10 )

If i replace the variable "twitbod" above with a string "fubar" it echos back as being a "data" item with the proper content-length.
However the variable twitbod echos back as being a "form" item, as below. I'm no HTTP expert, can anyone tell me why "twitbod" which is a string, is doing that? Since its already a string, do i need to convert it to something else to get it into the "data" item or is this normal behavior and data and form are interchangable?
thanks
{
"data": "",
"json": null,
"origin": "XX.XX.XX.XX",
"form": {
"oauth_consumer_key": "XXXXXXXX",
"oauth_signature_method": "HMAC-SHA1",
"status": "TestTweet1719",
"oauth_timestamp": "1384715839",
"oauth_nonce": "740051602",
"oauth_signature": "XXXXXXXXX",
"oauth_version": "1.0",
"oauth_token": "XXXXXXXXXX"
},
"args": {},
"url": "http://httpbin.org/post",
"headers": {
"User-Agent": "Roku/DVP-5.1 (025.01E01195A)",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "close",
"Accept": "*/*",
"Content-Length": "272",
"Host": "httpbin.org",
"Accept-Encoding": "deflate, gzip"
},
"files": {}
}
Kinetics Screensavers
0 Kudos
8 REPLIES 8
RokuChris
Roku Employee
Roku Employee

Re: http POST request

Might be an encoding issue. I know roURLTransfer is pretty picky about URLs being properly encoded. If you call SetURL() with a URL that isn't properly encoded, it won't work. The same might be true of POST data.
0 Kudos
squirreltown
Roku Guru

Re: http POST request

Thanks Chris, just to clarify, when you say URL, do you mean The whole request Including the POST body or just the actual address part?
thanks
Kinetics Screensavers
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: http POST request

"squirreltown" wrote:
Thanks Chris, just to clarify, when you say URL, do you mean The whole request Including the POST body or just the actual address part?


I don't know for sure. I don't recall ever seeing this exact issue.

I do know that a URL (the string passed to SetURL()) that isn't encoded correctly will fail. As a matter of habit, I always followup a call to SetURL() by printing the result of GetURL() to the console. If that result is blank, then I know SetURL() failed and it's almost always due to poor encoding.

My hunch is that maybe the same thing is happening to your POST data.
0 Kudos
squirreltown
Roku Guru

Re: http POST request

Chris, you were correct, I re-urlencoded my "twitbod" variable and it does now show up as data. However Twitter still doesn't like it (400 error).
Oddly, the string is exactly the same there is no additional encoding, the Roku just wanted it done again for some reason.

{
"data": "oauth_consumer_key=XXX&oauth_nonce=946393881&oauth_signature=XXX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1384961231&oauth_token=XXX&oauth_version=1.0&status=TestTweet2711",
"url": "http://httpbin.org/post",
"origin": "XX.XX.XX.XX",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "*/*",
"Host": "httpbin.org",
"Accept-Encoding": "deflate, gzip",
"Content-Length": "304",
"User-Agent": "Roku/DVP-5.2 (025.02E03264A)",
"Connection": "close"
},
"form": null,
"json": null,
"files": {},
"args": {}
}
Kinetics Screensavers
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: http POST request

"squirreltown" wrote:
Oddly, the string is exactly the same there is no additional encoding, the Roku just wanted it done again for some reason.


The server may be decoding it before showing it back to you. If it works now and didn't before and you didn't change anything else, you're definitely sending a different string.

It's important not to just encode the whole query string because you will be encoding the & and = characters that define the structure of the query. Those characters should not be encoded (http://www.ietf.org/rfc/rfc1738.txt). You'll get something like...

oauth_consumer_key%3DXXX%26oauth_nonce%3D946393881%26oauth_signature%3DXXX%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1384961231%26oauth_token%3DXXX%26oauth_version%3D1.0%26status%3DTestTweet2711


...which may allow the request to succeed, but will not be interpreted the way you want by the server.
0 Kudos
squirreltown
Roku Guru

Re: http POST request

"RokuChris" wrote:
...which may allow the request to succeed, but will not be interpreted the way you want by the server.


well thats a problem if i have to encode the string improperly to get the Roku to send it properly, that wont work,

Chris you are correct, doing urlencode(twitbod) is encoding the parts i dont want encoded
oauth_consumer_key%3DXXXX%26oauth_nonce%3D296381621%26oauth_signature%3DXXXD%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1384985154%26oauth_token%3D3XXXX%26oauth_version%3D1.0%26status%3DTestTweet554


this is the same Oauth string that retrieves the twitter feed just fine, 3 different ways, but when i send it via POST instead of GET the Roku puts it in the wrong place.

Is there a Roku engineer who would like to weigh in on this?'' Why wont the Roku send my just fine string (that works in plenty of other cases) in the "data" Item?


EDIT:
I tried string replace on my re-encoded variable and sure enough it outputs as "form". So, the "&" and "=" characters seem to cause this to happen. Why?
Kinetics Screensavers
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: http POST request

"squirreltown" wrote:
"RokuChris" wrote:
...which may allow the request to succeed, but will not be interpreted the way you want by the server.


well thats a problem if i have to encode the string improperly to get the Roku to send it properly, that wont work,


I don't think that's true. It's was encoded correctly, it just didn't represent what you wanted it to. This is all about how URLs are constructed. Reserved/unsafe characters within parameter values must be encoded or the server might interpret them wrong. But if you encode reserved characters that are not part of a parameter value, you may not end up sending the data you think you are.

Suppose I have...

foo1 = "abc&d"
foo2 = "efg=h"


And I want to send them as query parameters on a URL. If I do this...

foo1=abc&d&foo2=efg=h


...it's likely to confuse the server I send it to. And if I encode the whole thing, I get this...

foo1%3Dabc%26d%26foo2%3Defg%3Dh


...which is correctly encoded, but does not accurately represent my 2 parameters and their values. What I should do is this...

foo1=abc%26d&foo2=efg%3Dh
0 Kudos
squirreltown
Roku Guru

Re: http POST request

Thank You Chris.
Kinetics Screensavers
0 Kudos