Hi,
I think these are easy questions about the Node.js scripts feature in Backendless, but I can’t find the answer in the available docs or the example.
-
Is it correct to guess from the Shopping Cart example that for an HTTP POST, the JSON body is passed to the Node.js script in “request.body” and one just needs to parse “request.body” to get all the key-value pairs in the JSON as a javascript object? For example, the “additems.js” script has the line:
var jsonBody = JSON.parse( request.body );
-
When an HTML <form> is POSTed to a Node.js script with the attribute ‘enctype=“text/plain”’, are the form contents are already parsed into a javascript object attached to “request.body” member as they appear to be? That is can one get the javascript object of <form> contents as:
var formBody = request.body;
-
If one makes a GET request to a Node.js script with a list of query key-value parameters:
https://api.backendless.com/…?key1=value1&key2=value2
how are those query parameters passed to the script in the “request” object and how can one convert that list into a javascript object?
var queryBody = ???
Thanks Sergey.
I can add some info to 2) and 3).
-
As you point out, it would be better to send a request with a JSON body. Unfortunately, there are some circumstances where one has to POST a <form> though, and the only allowed “enctype” in HTML5 are:
i) application/x-www-form-urlencoded
ii) multipart/form-data
iii) text/plain
http://www.w3.org/TR/html5/forms.html#attr-fs-enctype
-
I’m wondering if there is a bug in query parameter processing for GET requests?
As the link you provide indicates, “req.query” is supposed to be a javascript object of parsed query parameters. Tests of Express 4.x on a local machine with a query string like “?a=x&b=y” do indeed show that “req.query” is a JS object (in JSON): {“a”:“x”, “b”:“y”}. But on Backendless server it seems as if the “&” may not be getting recognized properly and the result in “req.query” for the same query string is (in JSON): {“a”:“xb=y”}. Maybe some additional header elements indicating character set or something else are required?
Thanks
Rick,
Regarding the second question: when you submit a request using jQuery (or even XHR) you have full control over the header Content-Type, as a result, forcing to be application/json is never a problem.
Regards,
Mark