Add params to POST request body

Hey.
In my Roko channel I am sending a POST request to our server.
Is there a way to add params to POST request body (Not the header)?
It can be a single key-value or several of them. or a json object

Thanks,

save to tmp:/ and then roUrlRequest.postFromFile() ?

Thanks, but in this case, can you please explain how do I add header params and body params (json) to a request/file?

“dan_shneider” wrote:
Thanks, but in this case, can you please explain how do I add header params and body params (json) to a request/file?
regarding headers and cookies - that’s covered in https://sdkdocs.roku.com/display/sdkdoc/ifHttpAgent
re formatting body - no, can’t tell you off top of my head - but doing inspecting http in your browser (or Fiddler or packet sniffer) should put you on the right track

Thanks a lot

To add POST params to the request body, use ifUrlTransfer.AsyncPostFromString(request), where request is a string containing your POST parameters, url-escaped, separated by ampersands. To url-escape a parameter, call ifUrlTransfer.Escape(param).

“belltown” wrote:
To add POST params to the request body, use ifUrlTransfer.AsyncPostFromString(request), where request is a string containing your POST parameters, url-escaped, separated by ampersands. To url-escape a parameter, call ifUrlTransfer.Escape(param).
Oh, my bad for glancing past *PostFromString()! (what was i thinking? probably the query string but that’s part of the URL)

The non-Async version should work too, right?
Assuming one does not care about the response body (e.g. as often used as a REST action button).

“EnTerr” wrote:

“belltown” wrote:
To add POST params to the request body, use ifUrlTransfer.AsyncPostFromString(request), where request is a string containing your POST parameters, url-escaped, separated by ampersands. To url-escape a parameter, call ifUrlTransfer.Escape(param).
Oh, my bad for glancing past *PostFromString()! (what was i thinking? probably the query string but that’s part of the URL)

The non-Async version should work too, right?
Assuming one does not care about the response body (e.g. as often used as a REST action button).
Correct, PostFromString should work too.

Thank you all for your replies.
The thing is, I do need Async POST (I am using the data retrieved on the response).
My server expects several values in a json format on the request body.
I am having problems doing so.
I am creating a json string but on the request body is shows as HTML Form URL Encoded: application/x-www-form-urlencoded
This needs to be in format of application/json

stringForRequest = FormatJson(MY_JSON_STRING,1)…
request.AsyncPostFromString(stringForRequest)…

Any tips?

Solved:
mRequest.AddHeader(“Content-Type”, “application/json”)
mRequest.AddHeader(“Accept”, “application/json”)

could you show the code you wrote for this issue to resolve…because i have been through the same problem…thanks in advance…

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)

“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"}

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.

“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 … 0asIntegerasString

Hope that helps. :slightly_smiling_face:

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.

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

Thank you.

Still I can’t understand how to send body in post request