GET query parameters and POST body in Node.js scripts

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.

  1. 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 );

  2. 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;

  3. 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 = ???

Hi, Rick,

    Yes, this one is correct: if you send a request with a header "Content-Type: application/json" and send a JSON body then you can easily convert it to a Javascript object on server side using JSON.parse( request.body ). If you submit your form with enctype="text/plain", the request will be sent with a header "Content-Type: text/plain", which means that your request body will be passed as plain string, not JSON. Though as long as your request body string conforms to JSON standards you can still use JSON.parse( request.body ), I would recommend rather sending a request with Content-Type application/json explicitly. We use Express.js under the cover, so the request object is the Express' one. Thus you can use the Express.js documentation in order to find all it's properties and methods. Here is the part regarding your question about query params: [url=http://expressjs.com/4x/api.html#req.query]http://expressjs.com/4x/api.html#req.query[/url]
Regards, Sergey

Thanks Sergey.

I can add some info to 2) and 3).

  1. 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

  2. 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