Forum Discussion

slingxshot's avatar
slingxshot
Streaming Star
8 years ago

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!

2 Replies

  • 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's avatar
    slingxshot
    Streaming Star
    Looks it just needed this header 

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

    Thanks