Search code examples
apideveloper-toolsrokubrightscriptscenegraph

Send body in Post request in Brightscript Roku


I want to send the body in a post request in brightscript, can't find any way to do this, My code

xfer = CreateObject("roURLTransfer")
xfer.SetRequest("POST")

xfer.PostFromString("queryString")

xfer.SetCertificatesFile("common:/certs/ca-bundle.crt") 
xfer.AddHeader("Content-Type", "application/json")
xfer.AddHeader("Test-Header", "Test Header Value")
xfer.SetURL("https://d5c8-115-186-189-0.ap.ngrok.io/hello?title=hello&name=world")
queryString = xfer.Escape("?hello") + "&" + xfer.Escape("world")
print "Query String: " queryString
xfer.PostFromString(queryString)
xfer.InitClientCertificates()
rsp = xfer.GetToString()
json = ParseJson(rsp)

print "Rsp: " rsp
print "json: " json
print xfer.PostFromString(queryString)

I can send headers successfully, sending POST requests successfully, but the thing is, I can't send the body, can't figure out how to send the body, please help me, I am really stuck here, and not so many resources are available for Roku brightscript and their documentation is cold enough, I have read this whole page https://developer.roku.com/docs/references/brightscript/interfaces/ifurltransfer.md#ifUrlTransfer-PostFromString(requestasString)asInteger:3vqfjuna] please give me some helpful material to do this thing or give me any code snippet to do this, because, in the Roku Developer Forums, the solutions are not helpful, like this https://community.roku.com/t5/Roku-Developer-Program/How-to-pass-request-with-body-part-in-HTTP-POST-method-api-request/td-p/436560, from this link I have implemented different methods to send body, but not succeeded. Thanks in advance.


Solution

  • To send JSON as part of a POST request, just pass the JSON string data into the .AsyncPostFromString() function. Like this:

    body = { movieId: 123 }
    xfer.AsyncPostFromString(FormatJson(body))
    

    Here's an updated version of your example with these changes:

    requestBody = { movieId: 123 }
    xfer = CreateObject("roURLTransfer")
    
    xfer.SetRequest("POST")
    xfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
    xfer.AddHeader("Content-Type", "application/json")
    xfer.AddHeader("Test-Header", "Test Header Value")
    'send POST request to "Post Test Server" (a very useful server for testing stuff like this)
    'update this url accordingly
    xfer.SetUrl("http://192.168.1.22:8080")
    xfer.InitClientCertificates()
    
    'create a message port to monitor for the response
    port = createObject("roMessagePort")
    'assign the message port to the request
    xfer.SetMessagePort(port)
    
    'THIS is the way you include JSON in the POST body in the request
    'send the request in the background.
    xfer.AsyncPostFromString(formatJson(requestBody))
    
    'wait infinitely for the response
    response = wait(0, port)
    
    responseCode = response.GetResponseCode()
    responseBody = response.GetString()
    print responseBody
    

    You can run this Node.js code to verify that the server properly receives the body.

    var http = require('http');
    http.createServer(function (request, response) {
        let body = '';
        request.on('data', (data) => {
            body += data.toString();
        });
        request.on('end', () => {
            console.log('POST body: ', body);
        });
        response.writeHead(200, { 'Content-Type': 'text/html' });
        response.write('{"hello": "world"}');
        response.end();
    }).listen(8080);
    

    enter image description here