Doing multiplication with a scripted REST API

Lets create a simple REST API to explore the concept:

  1. Navigate to System Web Services > Scripted Web Services > Scripted REST APIs and create a new record. Use these details, and Save.
    • Name: Math
  1. In the Resources related list, click on New. Set the following fields:
    • Name: Multiply
    • HTTP method: get
    • Relative path: /multiply/{a}/{b}
    • Script: (Insert inside the provided function)
var body = {};
body.answer = request.pathParams.a * request.pathParams.b;
response.setBody(body);

This very simple script looks at two input variables, multiplies them, and sends the response back.

  1. Once saved, click on Explore REST API. Give a numerical value for a and b, click on Send, and the instance will do ...

Get ServiceNow: Building Powerful Workflows now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.