SAPUI5 OData v4 Batch Requests Header Parsing

910 views Asked by At

I'm trying to implement a UI5 service using an OData v4 backend. In general my bindings seem to work, however, there seems to be a problem parsing the headers of the batch requests. Parsing the batch parts by skipping the code in the debugger works fine.

In the console I get the following error:

2022-10-11 13:14:09.584899 $batch failed - Error: Expected 'OData-Version' header with value '4.0' but received value 'null' in response for http://localhost:8080/odata/$batch
    at h.doCheckVersionHeader (http://localhost:1337/resources/sap/ui/core/library-preload.js:4753:314)
    at Object.<anonymous> (http://localhost:1337/resources/sap/ui/core/library-preload.js:4786:415)
    at Object.<anonymous> (http://localhost:1337/resources/sap-ui-core.js:2099:9272)
    at p (http://localhost:1337/resources/sap-ui-core.js:2219:26833)
    at Object.fireWith [as resolveWith] (http://localhost:1337/resources/sap-ui-core.js:2219:27676)
    at y3 (http://localhost:1337/resources/sap-ui-core.js:2219:84906)
    at XMLHttpRequest.<anonymous> (http://localhost:1337/resources/sap-ui-core.js:2219:87536) sap.ui.model.odata.v4.ODataModel`

However, inspecting the headers in the developer tools the OData-Version header seems to be set

HTTP/1.1 202
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
OData-Version: 4.0
Content-Type: multipart/mixed;boundary=batch_63059dbf-3e96-4650-b6b9-c6237b7e3b9e
Content-Length: 3848
Date: Tue, 11 Oct 2022 11:14:06 GMT
Keep-Alive: timeout=60
Connection: keep-alive

In the function h.doCheckVersionHeader only the Content-Type and Content-Length are aviable.

I start my project via ui5 serve and seem to be using version 1.102.1. Does anyone know how I can get the requests to work?

2

There are 2 answers

0
Gleydar On

I have resolved the issue by adding the Access-Control-Expose-Headers header to the response: https://github.com/SAP/openui5/issues/3613#issuecomment-1274609280

0
Boghyon Hoffmann On

The section "Response Headers" from the documentation topic "Model Instantiation and Data Access" now lists headers that need to be added to the Access-Control-Expose-Headers response header:

The OData model processes some of the response headers, namely:

  • DataServiceVersion (only when consuming an OData V2 service),
  • Date,
  • ETag,
  • OData-Version,
  • Preference-Applied,
  • Retry-After,
  • SAP-Messages,
  • X-CSRF-Token.

Some SAP applications will also require the processing of SAP-ContextId, SAP-Err-Id, and SAP-Http-Session-Timeout. When using cross-origin resource sharing (CORS), it is important to add all these headers to the Access-Control-Expose-Headers response header.

Also, make sure that values assigned to the headers are syntactically valid. E.g. no dangling characters: OData-Version: 4.0;OData-Version: 4.0.