Roku Developer Program

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

Re: Add params to POST request body

could you show the code you wrote for this issue to resolve......because i have been through the same problem...thanks in advance...
0 Kudos
Highlighted
tim_beynart
Level 7

Re: Add params to POST request body

Using JSON formatting functions will not work, you have to generate your POST JSON manually using Chr(34) to insert the quotation marks.

Value1="some value"
Value2="some other value"
post="{"+Chr(34)+"Key1"+Chr(34)+":"+Chr(34)+Value1+Chr(34)+","+Chr(34)+"Key2"+Chr(34)+":"+Chr(34)+Value2+Chr(34)+"}"

If http.AsyncPostFromString(post) Then
   event = Wait(timeout, http.GetPort())
   If Type(event) = "roUrlEvent" Then
      response = event.GetString()
   Else If event = invalid Then
       ? "AsyncPostFromString timeout"
       http.AsyncCancel()
   Else
       ? "AsyncPostFromString unknown event", event
   End If
End If

if response <> invalid AND response <> ""
   json = parseJSON(response)
0 Kudos
Highlighted
Roku Employee
Roku Employee

Re: Add params to POST request body

"tim_beynart" wrote:
Using JSON formatting functions will not work, you have to generate your POST JSON manually using Chr(34) to insert the quotation marks.


Tim, can you be more specific about what is not working for you?
Certainly, you do not have to manually format JSON in the general case.

The only problem I can guess that you are running into is with respect to associative array key case preservation,
which might be an issue if you are assigning fields using a.Key=Value instead of a["Key"]=Value or a.AddReplace("Key", Value).


Value1="some value"
Value2="some other value"
a={}
a["Key1"]=Value1
a["Key2"]=Value2
? FormatJSON(a)
'==>
{"Key1":"some value","Key2":"some other value"}
0 Kudos
Highlighted
tim_beynart
Level 7

Re: Add params to POST request body

Tim, can you be more specific about what is not working for you?
Certainly, you do not have to manually format JSON in the general case.

It's been quite some time since I messed with it, but IIRC, my request body was blank if I tried to use an AA converted to JSON using formatJSON. The case is not an issue, I wrote that example off the top of my head. I can try to dig up an example to demonstrate.
0 Kudos
Highlighted
Roku Employee
Roku Employee

Re: Add params to POST request body

"tim_beynart" wrote:

It's been quite some time since I messed with it, but IIRC, my request body was blank if I tried to use an AA converted to JSON using formatJSON. The case is not an issue, I wrote that example off the top of my head. I can try to dig up an example to demonstrate.


As another attempt at psychic debugging, I'm guessing that your call to FormatJSON was failing and returning an empty string.  
This doesn't have anything to do with the roUrlTransfer or POST part of your code, but could happen if you had non-POD (plain old data) values in the AA, for example, or something that was not directly supported by JSON.
I would expect that you would see an error message printed in the app debug console in that case.

https://sdkdocs.roku.com/display/sdkdoc ... 0asInteger)asString

Hope that helps.   Smiley Happy
0 Kudos
Highlighted
parag
Level 7

Re: Add params to POST request body

I have the same question. I have a request with a bunch of query params that I am try to post and I get a request timeout with everything that I have tried.
url = CreateObject("roUrlTransfer")
1. url.SetUrl("http://www.google.com/tesing?userId=test1&sessionId=test2&categoryId=test3")

2. url.SetUrl("http://www.google.com/testing")
   request=url.Escape("userId=test1&sessionId=test2&categoryId=test3")

3. a = {}
   a["userId"]="test1"
   a["sessionId"]="test2"
   a["categoryId"]="test3"
   request=url.Escape(FormatJson(a))

url.AsyncPostFromString(request) in #2 and #3 and url.AsyncGetToString() in #1 all return a -7 response code. Any pointers to what I am doing wrong here? A rest client gives a response with both post/get.
0 Kudos
Highlighted
Roku Employee
Roku Employee

Re: Add params to POST request body

Your issue is encoding the full string, where & and = should be as-is. Something like this should do it (typed but not tested):

function params_to_query_string(params)
   lst = []
   for each item in params.items():
       lst.push(item.key.escape() + "=" + item.value.escape())
   next
   return lst.join("&")
end function

when called with your
a = {
   userId: "test1"
   sessionId: "test2"
   categoryId: "test3"
}
0 Kudos
Highlighted
parag
Level 7

Re: Add params to POST request body

Thank you.
0 Kudos