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: 
slingxshot
Streaming Star

roUrlTransfer send POST as JSON

I was wondering how I would send the data as JSON. If anyone has any examples.

I am trying to basically duplicate this... I am guessing this javascript sample is sending data as JSON?

var templateXHR = new XMLHttpRequest();
templateXHR.responseType = "json";
templateXHR.addEventListener("load", function() {  }, false);
   templateXHR.open("POST", checkIfReadyURL, true);
   templateXHR.send("code="+deviceCodeResult+"&grant_type=http://oauth.net/grant_type/device/1.0&client_id=ott&client_secret=123");

Thanks!
0 Kudos
2 REPLIES 2
tim_beynart
Channel Surfer

Re: roUrlTransfer send POST as JSON

This is some edited code from a project, might be buggy, didn't test.
http = CreateObject("roUrlTransfer")
http.RetainBodyOnError(true)
messagePort = CreateObject("roMessagePort")
http.SetPort(messagePort)
http.setCertificatesFile("common:/certs/ca-bundle.crt")
http.InitClientCertificates()

http.addHeader("Some-Random-Header","header stuff")
http.SetUrl("http://something")

post = {
foo:"hello",
bar:"world"
}
postJSON = FormatJson(post)

response=""
lastresponsecode = ""
lastresponsefailurereason = ""
responseheaders = []
if http.AsyncPostFromString(postJSON) then
  event = Wait(10000, http.GetPort())
  if Type(event) = "roUrlEvent" Then
    response = event.getString()
    responseheaders = event.GetResponseHeaders()
    lastresponsecode = event.GetResponseCode()
    lastresponsefailurereason = event.GetFailureReason()
  else if event = invalid then
    http.asynccancel()
    lastresponsefailurereason = "HTTP timed out. Configured Timeout: 10s"
    lastresponsecode = 0
  Else
    ? "AsyncPostFromString unknown event"
  end if
end if

? "Response Headers: ";responseheaders
? "Response Code: ";lastresponsecode
? "Failure Reason: ";lastresponsefailurereason
? "Response: ";response

0 Kudos
slingxshot
Streaming Star

Re: roUrlTransfer send POST as JSON

Looks it just needed this header 

response.AddHeader("content-type", "application/x-www-form-urlencoded")

Thanks
0 Kudos