Create REST API in ServiceNow

Опубликовано: 31 Июль 2026
на канале: Servicenow Technology
470
15

REST : Representational State Transformation

Representational - Same data/resource we can represent in different ways
State - state of the data
Transfer - exchanging data

API - Application Programming Interface (Interface that allow us to connect two different application)

We create interface to connect to the database to send/receive data over the internet through HTTP protocol.

Rest is all about managing resources(CRUD).

1. https://abc.com/api/contacts (GET - to fetch records)
2. https://abc.com/api/contacts (POST - to add record)
3. https://abc.com/api/contacts (PUT - to update record) - it can perform UPSERT operation
4. https://abc.com/api/contacts/contact_id (PATCH - to update record) - it cannot perform USERT operation
5. https://abc.com/api/contacts/contact_id (DELETE - to delete record)

PATH parameters:
Variable in the path parameters helps in pointing towards specific resources. // request.pathParams


QUERY parameters
Variable in the query parameters helps to filter through list of resources. // request.queryParams

https://abc.com/api/contacts?limit=20...


offset = starting point
limit = how many records you want


Status code help us to understand what has happened with our request.

100 - Informational Status
200 - Success Status
300 - Redirectional status
400 - Client Side Error
500 - Server Side Error


200 = Resource has been updated/created and it can return all information.
201 = Resource has been updated/created however it will return few details.
400 = Fault with client side.(Invalid request payload or headers)
409 = Entity you are creating already exists in the system.
404 = Not found error


To handle REST Error

var sn_ws_err=new sn_ws_err.ServiceError(); // Custom errors

// setStatus(code);
// setDetails(str/obj/num);
// setMessage(message);
----------------------------------response.setError(sn_ws_err)


// new sn_ws_err.BadRequest('error message'); // 400 response.setError(new sn_ws_err.BadRequest('error message'));
// new sn_ws_err.ConflictError('error message'); // 409
// new sn_ws_err.NotFoundError('error message'); // 404

What are HTTP headers?
Answer: Headers are part of request which is used to exchange metadata with respect to the request, which doesn't have anything to do with actual resources.