OK so why are the request parameters with complex ...
# suitecommerce
m
OK so why are the request parameters with complex objects so poorly parsed in the Backend Service
context.request
compared to a Suitlet or RESTlet script? Here is an example: I am getting the cart items and sending it to the backend as an object. Usually as a POST I would catch by doing
JSON.parse(request.body)
.
Copy code
cartComponent.getLines()
                    .then((lines) => {
                        this.model
                            .fetch({
                                type: "post",
                                data: {
                                    action: "updateOrder",
                                    session: this.session,
                                    lines: lines

                                }
                            })
                            .done(response => {
                                console.debug({ response: response });
                            })
                            .catch(reason => {
                                console.error({ title: "ERROR_UPDATING_ORDER", reason: reason });
                            });
                    });
I have tried logging request in the backend and both aren't well structured. I have tried logging the body.
Copy code
function doPost(context) {
    log.debug({
        title: "doPost body audit",
        details: {
            body: context.request.body
        }
    });

    log.debug({
        title: "doPost body JSON audit",
        details: {
            body: JSON.parse(context.request.body)
        }
    }); // fails with error SyntaxError: Unexpected token:
}
This is essentially what I get.
Copy code
{"body":"action=updateOrder&session%5Borderid%5D=2550927&session%5Btranid%5D=SO76470&session%5Bshipdate%5D=7%2F5%2F2022&session%5Bmode%5D=editorder&lines%5B0%5D%5Bextras%5D%5Brate_formatted%5D=%240.00&lines%5B0%5D%5Bextras%5D%5Btax_rate1%5D=&lines%5B0%5D%5Bextras%5D%5Btax_type1%5D=&lines%5B0%5D%5Bextras%5D%5Btax_rate2%5D=&lines%5B0%5D%5Bextras%5D%5Btax_type2%5D=&lines%5B0%5D%5Bextras%5D%5Btax1_amount%5D=&lines%5B0%5D%5Bextras%5D%5Btax1_amount_formatted%5D=%240.00&lines%5B0%5D%5Bextras%5D%5Bdiscount%5D=0&lines%5B0%5D%5Bextras%5D%5Bpromotion_discount%5D=&lines%5B0%5D%5Bextras%5D%5Btotal%5D=0&lines%5B0%5D%5Bextras%5D%5Bfree_gift%5D=false&lines%5B0%5D%5Bextras%5D%5Bamount_formatted%5D=%240.00&lines%5B0%5D%5Bextras%5D%5Btax_amount_formatted%5D=%240.00&lines%5B0%5D%5Bextras%5D%5Bdiscount_formatted%5D=%240.00&lines%5B0%5D%5Bextras%5D%5Btotal_formatted%5D=%240.00&lines%5B0%5D%5Binternalid%5D=item8518set165186&lines%5B0%5D%5Bquantity%5D=2&lines%5B0%5D%5Brate%5D=0&lines%5B0%5D%5Bamount%5D=0&lines%5B0%5D%5Bitem%5D%5Bextras%5D%5Bisinstock%5D=true&lines%5B0%5D%5Bitem%5D%5Bextras%5D%5Bweightunit%5D=...
I have tried logging the parameters.
Copy code
function doPost(context) {
    log.debug({
        title: "doPost parameters audit",
        details: {
            parameters: context.request.parameters
        }
    });
}
This is what I get.
Copy code
{"parameters":{"lines[1][item][extras][ispurchasable]":"true","lines[0][item][extras][onlinecustomerprice_detail][onlinecustomerprice_formatted]":"$0.00","lines[1][item][extras][saleunit]":"Case of 6","lines[0][item][extras][keyMapping_stock]":"quantityavailable","lines[1][item][extras][keyMapping_comparePriceAgainstFormated]":"$4.56","lines[0][item][extras][keyMapping_sku]":"800232","lines[0][item][extras][pricelevel6_formatted]":"$0.00","lines[0][item][extras][weightunit]":"lb","lines[1][item][extras][keyMapping_id]":"internalid","session[orderid]":"2550927","lines[1][item][extras][storedisplayname2]":"100483","lines[0][item][extras][pricelevel1]":"0","lines[0][item][extras][keyMapping_pageTitle][]":"pagetitle","lines[1][rate]":"4.56","lines[0][item][extras][itemimages_detail][squash][altimagetext]":"","lines[0][item][extras][keyMapping_url]":"/product/8518","lines[1][item][extras][keyMapping_outOfStockMessage]":"","lines[0][item][isinactive]":"false","lines[1][item][itemtype]":"InvtPart","lines[1][extras][tax1_amount]":"","lines[1][extras][tax1_amount_formatted]":"$0.00","lines[0][item][extras][keyMapping_showInStockMessage]":"false","lines[0][item][displayname]":"...
How would you parse a POST request body that has a complex object?
c
Have you tried
request.getParameter("xyz");
?
In other words, get a handle on what you posted, then you can parse it.
m
Creates an error.
Copy code
Cannot find function getParameter in object http.ServerRequest
c
Copy code
data: { $.param({
    action: "updateOrder",
    session: this.session,
    lines: lines })
Try sending it up to the back-end like that.
m
Came out the same on the backend.
OK I think I figured it out. Send like this.
Copy code
data: JSON.stringify({
    action: "updateOrder",
    session: this.session,
    lines: lines })
Catch like this.
Copy code
const params = JSON.parse(JSON.parse(context.request.body));
👍🏻 2
c
Gotta turn your object into text or you send up some weird representation of the object.
👍🏻 1
m
LOL...yeah it became obvious at certain point.