SuiteCommerce Service GET and POST data


How to retrieve GET parameters and POST body data

When making requests to your service you need to access the incoming data. This post will show you how retrieve GET and POST data from the SuiteCommerce service context object.

SuiteCommerce service context object

In a back end service file you get an entry point declared by a service method. The service method contains one parameter named ctx. This is the context object, which is very similar to the https.ServerRequest object from SuiteScript 2.0.

GET request Parameters

If your GET request contained an internalid parameter, you access it through the request object like below.

service: function (ctx) {
  // to access GET parameters you use the parameters object
  const id = ctx.request.internalid;
}
POST request Body Data

The POST data is embedded in the request.body object. You can access it like this.

service: function (ctx) {
  // posted a json object with an internalid property
  const params = JSON.parse(ctx.request.body);
  const id = params.internalid.
}

Conclusion

The context (ctx) object passed into the service method gives you access to all sorts of helpful server request goodies in the SuiteScript 2.0 SuiteCommerce back end. Accessing the GET parameters and POST body objects is just the tip of the iceburg.