Reading HTTP Header Parameters in an OData Service

Reading HTTP Header Parameters in an OData Service. Adding Custom Parameters to the HTTP Header

In this note, I want to demonstrate an implementation approach that allows reading HTTP header parameters in an OData service associated with your SAPUI5 application.

What is an HTTP Header?

See: HTTP Headers

HTTP headers let the client and server exchange additional information with an HTTP request or response. An HTTP header consists of a case-insensitive name followed by a colon (:) and its value. Whitespace before the value is ignored.

Example of Accessing the HTTP Header from an OData Service

To access the HTTP header directly within the OData service, you can add the code below to the relevant method of the *DPC_EXT class that is called from the SAPUI5 application.

See: Data Provider Class

See the note: Creating a Web Application Using the SAPUI5 Framework (6), which discusses an example of creating an OData service for an SAPUI5 application.
DATA: lt_request_header TYPE tihttpnvp,
          ls_request_header LIKE LINE OF lt_request_header.
 
    LOOP AT  me->mr_request_details->technical_request-request_header ASSIGNING FIELD-SYMBOL(<fs_req_header>).
    
      ls_request_header-name = <fs_req_header>-name.
      ls_request_header-value = <fs_req_header>-value.
      APPEND VALUE #( name = <fs_req_header>-name
                                  value =  <fs_req_header>-value )
      TO lt_request_header.
    
    ENDLOOP.

The video fragment below shows the sequence of actions resulting in the population of the internal table lt_request_header, which contains the HTTP header data after the request from the SAPUI5 application is processed in the backend system.

0:00
/0:40

Example of Defining Custom Parameters in the HTTP Header

To define custom parameters in the HTTP header, use the setHeaders method of the sap.ui.model.odata.v2.ODataModel class before making a request to the backend.

See: class sap.ui.model.odata.v2.ODataModel
See: setHeaders

For example:

var sUrl = "/sap/opu/odata/SAP/ZAPP12_SRV";
var odataModel = new sap.ui.model.odata.v2.ODataModel(sUrl, {
                json: true,
                loadMetadataAsync: true
            });
 
            var oModel = new sap.ui.model.json.JSONModel();
 
            odataModel.setHeaders({
                "myCustomparam1": "userPressed",
                "myCustomparam2": new Date(Date.now())
            });

Verifying Access to Custom Parameters Defined in the SAPUI5 Application

This is how you verify the presence of parameters added to the HTTP header by the SAPUI5 application.

0:00
/0:49