Show TOC

Building an OData RequestLocate this document in the navigation structure

An OData request consists of the following components:

  • The endpoint (URL) of the request with service root URI, resource path, and query options
    Example http://www.sapjam.com/api/v1/OData/Groups?$orderby=CreatedAt

    The endpoint consists of the following parts:

    • Service root URI: http://www.sapjam.com/api/v1/OData
    • Resource path: /Groups
    • Query options: /$orderby=CreatedAtGroups
  • (Optional): Header fields such as Content-Type

    If not supplied, by default, ABAP SMI uses Content-Type=application/xml.

  • (Optional): A request body such as the XML representation of an entity to be created
  • The HTTP method: GET, POST, PUT/PATCH, DELETE
Note The metadata document <ServiceRootURI>/$metadata describes the Entity Data Model (EDM) for the underlying OData service. This model describes the OData resources (entities, entity sets, properties, navigations) exposed by the OData service. When building your endpoints, you can use the metadata document as a reference on how to access specific OData resources.

In ABAP SMI, an OData request is represented by the interface IF_ODL_REQUEST. You can get request objects using the ABAP SMI API instance (method get_request in interface IF_ODL_API). The method get_request expects a string parameter iv_toplevel_resource that describes the name of the top-level resource of the request.

Example An ABAP SMI API instance is assigned to the variable lo_api. The following code snippet creates a request with a top-level entity set “Groups”.
DATA lo_request TYPE REF TO if_odl_request.
lo_request = lo_api->get_request(
  iv_toplevel_resource = 'Groups'
).				
The object lo_request represents a request with the endpoint /Groups. The base URL (for example, www.sapjam.com) and OData service root (for example, api/v1/OData) are defined in Customizing and thus by the API instance lo_api. The complete URL of the request would be:
<ServiceRootURI><Endpoint>
See our example:
http://www.sapjam.com/api/v1/OData/Groups 

The endpoint of the request object can be further built by the following methods defined in interface IF_ODL_REQUEST:

Method Description
navigate This method extends the resource path of the OData request with an entity or entity set. The name of that entity or entity set needs to be passed using the iv_name method parameter.
navigate_by_key This method extends the resource path of the OData request by a key consisting of one or more name/value pairs. The key needs to be passed on through the it_key method parameter. The table type of this key is specified by the tt_key type provided by this interface. To be semantically correct, the resource path needs to point to an entity set to which the key can be applied.
navigate_by_simple_key This method extends the resource path of the OData request by a simple key. The name and the value of this simple key need to be passed on through the iv_name and iv_value method parameters.
expand This method extends the query options of the OData request by an $expand clause. The iv_navprop method parameter specifies the name of the navigation property that points to the entity to be expanded.
count This method extends the OData request by the $count system query option.
value This method extends the OData request by the $value system query option.
inlinecount This method extends the OData request by the $inlinecount system query option. The optional iv_value method parameter specifies the value of $inlinecount, for example, allpages or none. If a value for iv_value is not supplied, the default value allpages is used.
append_query_option

This method appends a query option string to the resource path. The query option string is specified by the iv_string method parameter.

For example: $filter=(MemberCount gt 5)

Examples

Here are some examples of how to build requests:

  • Endpoint …/Groups(Id='aBc123xyZ')/Creator
    lo_request = lo_api->get_request(
    	iv_toplevel_resource = 'Groups'
    ). 
    lo_request->navigate_by_simple_key( iv_name = 'Id' iv_value = '''aBc123xyZ''' ). 
    lo_request->navigate( 'Creator' ).					
  • Endpoint …/Groups/$count
    lo_request = lo_api->get_request(
    	iv_toplevel_resource = 'Groups'
    ). 
    lo_request->count( ).				
  • Endpoint …/Groups?$orderby=CreatedAt
    lo_request = lo_api->get_request(
    	iv_toplevel_resource = 'Groups'
    ).
    lo_request->append_query_option( '$orderby=CreatedAt' ).