0

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.

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26

1 Answers1

0

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

TwitchBronBron
  • 2,783
  • 3
  • 21
  • 45
  • The answer you have mentioned, I had already read it, you have described that how to receive body from the backend, but my question is how to send some data in body from the frontend (Roku device) to the backend server in Post request, like this POST http://example.com/api/post content-type: application/json { "username": "john", "userage": 24 } forget about how to read the body response from the body, I want to know that how to send data in body to backend server – aleem aheer Mar 11 '23 at 12:23
  • Oh, sorry about that. I just updated my answer to better solve your problem. Does that work? – TwitchBronBron Mar 14 '23 at 12:02
  • This is also not working, I tried 100 percent the same code you provided, but it doesn't work. – aleem aheer Mar 15 '23 at 12:11
  • 1
    I finally had a chance to actually test this code. I had a wrong variable name so I updated my example to fix that. Also, I added a url to the "Post Test Server" website (https://ptsv3.com/) so the example will work entirely on its own, which helps rule out problems with your own servers to prove the example works. Please give it a try now. – TwitchBronBron Mar 16 '23 at 09:40