Extracting Parameters in a SuiteCommerce Service File for a "GET" and "POST" Request

in , November 22nd, 2024
clear glass tea cup beside keyboard

In this short tutorial article, we will go over how to extract parameters in a SuiteCommerce service file for a "GET" and "POST" request.

About "Get and "Post" Requests to Service Files

Both “GET” and “POST” requests to service files can contain data the requester passed along. Extracting this data is done differently based on the request. See the tutorial article SuiteScript2(SS2) Services for SuiteCommerce for the different ways to make a request.

Best Practice: Add validation of the incoming parameters!

The code block below shows how to extract parameters passed for each request type. Remember, “fetch” calls make a “GET” request, and “save” calls make a “POST” request (more info here).

Code Example: How to Extract the Parameters

define([], function () {
    "use strict";


    var response = {
        errors: [],
    };


    function service(context) {
        try {
            //Extract parameters from a "POST" request
            if (context.request.method == "POST") {
                var parameters = context.request.body ? JSON.parse(context.request.body) : {};
            }
            //Extract parameters from a "GET" request
            else if (context.request.method == "GET") {
                var parameters = context.request.parameters || {};
            }
        } catch (error) {
            response.errors.push(error);
        }


        context.response.write(JSON.stringify(response));
    }


    return {
        service: service,
    };
});

Author: Sam Gagliardi

Got stuck on a step in this article?

We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!

Oracle NetSuite Alliance Partner & Commerce Partner

If you have questions about how our team can support your business as you implement NetSuite or SuiteCommerce, feel free to contact us anytime. Anchor Group is a certified Oracle NetSuite Alliance Partner and Commerce Partner equipped to handle all kinds of NetSuite and SuiteCommerce projects, large or small!

 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart