Code examples · OData · Beginner
OData V4: read, filter, create and update with HTTP and JavaScript
The problem
A web team needs to read and change leave requests from their own app.
How we solve it
- 1URL anatomyservice, entity set, key
- 2Query options$filter $select $orderby
- 3Write safelyCSRF + ETag
The result
A tested set of HTTP requests plus a small Node.js script that fetches a token, creates a request and updates it.
1. The requirement
A web team needs to read and change leave requests from their own app. They must know the exact URL shapes, how to filter on the server, and how to write safely (CSRF token, ETag).
2. How we design it
Learn the contract first by reading the service document and $metadata, then let the server do the work with query options so only the needed rows and columns travel. For writes, follow the safe sequence: fetch a CSRF token and session cookie, send the change, protect it with the ETag.
Why we decided it this way
- 1Read $metadata instead of guessingField names, key types and action namespaces are only reliable there.
- 2Filter, select and page on the serverLess data over the wire, and the same query works for a UI and a script.
- 3CSRF token plus session cookieSAP services reject changes without both. The token belongs to the session it was issued for.
- 4If-Match with the ETagA stale update fails with 412 instead of silently overwriting someone else's change.
Before you start
- A published OData V4 service. This example assumes the Leave Request service from the RAP example, but any V4 service works.
- Your service root, for example https://<host>/sap/opu/odata4/sap/zui_leaverequest_o4/srvd/sap/zui_leaverequest/0001/ (the exact path is shown in the service binding).
- Node.js 20+ for the script, and a way to authenticate (a technical user or token).
3. The code, step by step
Step 1 of 3: Know the URL anatomy
Learn the three URLs every OData service has: the service document, the metadata and an entity set.
# Service document
# Replace {root} with your service root, ending in a slash
GET {root}
Accept: application/json
# $metadata
GET {root}$metadata
Accept: application/xml
# Entity set and single entity
GET {root}LeaveRequest
GET {root}LeaveRequest(4d1e8c1a-7d52-4f0c-9d0f-5c2a9d6b1e11)
Part by part (plain text)
- Service document. Lists the entity sets the service offers. Start here to see what is available.
- $metadata. The full contract in XML: entity types, key fields, data types, associations and actions. When a field name or action namespace is unclear, read it here, never guess it.
- Entity set and single entity. An entity set is a collection. Add the key in round brackets for one entity. UUID keys are written without quotes in OData V4.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
A tested set of HTTP requests plus a small Node.js script that fetches a token, creates a request and updates it.
What you should see
GET with $filter returns only matching rows and only the selected columns. The script prints 'Created <uuid> N' and then 'Updated end date'. Changing the same row again with the old ETag returns 412 Precondition Failed.
Try it and check the result
- First paste the GET requests into a REST client (for example the VS Code REST Client extension or Postman) and confirm the service document and $metadata load.
- Run the $filter request with Status eq 'N'. Rows created by the RAP example should appear.
- To see 412: GET the entity and copy its @odata.etag. PATCH it once (this changes the ETag). Then PATCH again with the old If-Match value and you get 412 Precondition Failed, which proves optimistic locking works.
- If POST returns 403, the CSRF token or session cookie was missing. If it returns 400 with a message, read error.message: it is the business object's validation text.
Common mistakes
- Sending the CSRF token without the cookie from the same response gives 403.
- Using OData V2 syntax in V4: V4 uses $count=true (not $inlinecount) and keys without quotes for GUIDs.
- Never put credentials in the script file or commit them. Use environment variables or a secrets manager.
- Do not guess names. Actions have a namespace shown only in $metadata; copy it from there.