Your Digital Media Has Never Looked So Good

 
slingxshot
Topic Author
Posts: 28
Joined: Tue Jul 14, 2015 8:58 am

roUrlTransfer send POST as JSON

Tue Nov 07, 2017 8:18 pm

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!
 
tim_beynart
Posts: 170
Joined: Wed Jul 15, 2015 8:30 am

Re: roUrlTransfer send POST as JSON

Wed Nov 08, 2017 11:01 am

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

 
slingxshot
Topic Author
Posts: 28
Joined: Tue Jul 14, 2015 8:58 am

Re: roUrlTransfer send POST as JSON

Wed Nov 08, 2017 2:21 pm

Looks it just needed this header 

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

Thanks

Who is online

Users browsing this forum: No registered users and 9 guests