ERPClimb logoERPClimb
SAP Gateway
RAP / CDS / ODataadvanced 9 min read

SAP Gateway Part 4: Paging, Filtering, CSRF and Authorization

Preview · a free account unlocks the full lesson

Key takeaway

Implement Gateway services that behave well in production: server-side paging ($top/$skip), filtering and sorting via the request context, $inlinecount, CSRF handling, and authorization checks per operation.

In this lessonShow sections

Visual overview

SAP Gateway Part 4: Paging, Filtering, CSRF and Authorization — learning sheetFlow: Replace SELECT * in GET_ENTITYSET with the pattern in the code example → Test $filter, $orderby, $top/$skip and $inlinecount in GW client → Compare runtime with ST05 before and after → Add AUTHORITY-CHECK per operation. Key ideas: THE ANTI-PATTERN; READ QUERY OPTIONS FROM THE REQUEST CONTEXT; SORT BEFORE PAGING; CSRF. Watch out: Never disable CSRF protection to "fix" 403 errors; fix the client instead.ERPClimb.com / RAP / LESSON 4/4 / LEARNING SHEETSAP Gateway Part 4: Paging, Filtering, CSRF andAuthorization1 · HOW IT FLOWS1Replace SELECT *in GET_ENTITYSETwith the pattern…2Test $filter,$orderby,$top/$skip and…3Compare runtimewith ST05 beforeand after4AddAUTHORITY-CHECKper operation2 · KEY IDEAS EXPLAINEDThe Anti-PatternSELECT * FROM zpr_head INTO TABLE et_entityset. —then the framework applies $top/$skip in memory("generic paging"). With 2 million rows…Read Query Options From The Request…WHERE:io_tech_request_context->get_osql_where_clause_convert() returns an ABAP SQL WHERE clause built from…Paging: io_tech_request_context->get_top( ) andget_skip( ).Sort Before Paging$skip/$top without a stable order return randompages. Always add a default ORDER BY (e.g. by key)when the client sends none.CsrfGateway requires an X-CSRF-Token for modifyingrequests: clients fetch it with GET +"X-CSRF-Token: Fetch".UI5 models do this automatically; custom clientsmust implement it.3 · REMEMBER · WATCH OUTRememberGET_ENTITYSET must apply $filter, $orderby, $top and $skip in thedatabase SELECT — not after reading everything.Use io_tech_request_context: get_osql_where_clause_convert, get_top,get_skip, get_orderby.Fill es_response_context-inlinecount when $inlinecount=allpages isrequested.!Watch outNever disable CSRF protection to"fix" 403 errors; fix theclient instead.4 · IN A REAL PROJECTA purchase request list app became slower every month. ST05 showed that each request read the entire ZPR_HEADtable (1.8 million rows) because GET_ENTITYSET used SELECT * and relied on generic paging. The teamimplemented filtering, sorting, paging and inline count from the request context, with a default sort by key…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 1 through 3 built and registered a Gateway service. The most common production problem with SEGW services is that GET_ENTITYSET selects everything and ignores the query options the client sent, and this part fixes that.

The anti-pattern looks like SELECT * FROM zpr_head INTO TABLE et_entityset, after which the framework applies $top and $skip in memory — so-called generic paging. With two million rows in the table, every single list request ends up reading all two million rows before the framework throws most of them away.

The fix is to read the query options from the request context instead of letting the framework guess. io_tech_request_context->get_osql_where_clause_convert( ) returns an ABAP SQL WHERE clause built from $filter, with property names already mapped to ABAP field names; because it is generated safely by the framework from the model, it can be used directly in a dynamic WHERE.

Paging comes from io_tech_request_context->get_top( ) and get_skip( ), and sorting from get_orderby( ), which you map to an ORDER BY clause. For the count, call io_tech_request_context->has_inlinecount( ) and, when it is set, run a COUNT with the same WHERE clause and fill es_response_context-inlinecount so the client knows the total result size for $inlinecount=allpages.

If you only want to read the columns the client actually asked for, get_select_with_mandtry_fields( ) gives you the $select list.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Referenced SAP objects

Reviewed reference pages for the objects this page mentions.

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.