sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

The IODataBatchOptions interface does not extend the RequestInit interface

Open vman opened this issue 10 years ago • 4 comments

This means that right now, there is no way to override the default HTTP headers sent with an OData batch request. Similar issue was discussed in another thread #44

iodatabatchoptions

As you can see, the IODataBatchRequestOptions does indeed extend the RequestInit interface and hence, an individual request in a batch can be set to override the headers. But since the entire batch is sent with an odata-version 4.0 header, the batch request fails and returns malformed data.

Here is my code for my ODataBatch request:

private _makeODataBatchRequest(): void {
    const serviceScope: ServiceScope = ServiceScope.startNewRoot();
    serviceScope.finish();

    const webAbsoluteUrl: string = this.context.pageContext.web.absoluteUrl;
    const batchOpts: IODataBatchOptions = {
        webUrl : webAbsoluteUrl
        //Need a way here to override default options e.g Headers.
    };
    const odataBatch: ODataBatch  = new ODataBatch(serviceScope, batchOpts);

    const getMyProps: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/SP.UserProfiles.PeopleManager/GetMyProperties`);

    //Overriding the header for a single request in the batch does not work as the entire batch is sent with odata-version:4.0
    const reqHeaders: Headers = new Headers();
    reqHeaders.append('odata-version', '');
    const performSearch: Promise<Response> = odataBatch.get(`${webAbsoluteUrl}/_api/search/query?querytext='sharepoint'`, { headers: reqHeaders });

    odataBatch.execute().then(() => {

      getMyProps.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });

      performSearch.then((response: Response) => {
        response.json().then((responseJSON) => {
          console.log(responseJSON);
        });
      });
    });
  }

vman avatar Aug 29 '16 14:08 vman

Tracked as VSO#242882

manishgarg1 avatar Aug 30 '16 17:08 manishgarg1

This is by design:

  • It does not make sense to allow you to customize the headers or other request parameters for the overall batch REST call, since it would complicate our algorithm
  • It does make sense to allow you to customize the headers for individual REST calls, which is why we provide IODataBatchRequestOptions
  • However, certain headers are not valid inside a batch. For example, the ODataBatch uses OData Version 4.0, and SharePoint does not allow you to use OData Version 3.0 requests inside an OData Version 4.0 batch. Therefore, your "odata-version" header above is not valid.

It would be possible for ODataBatch to detect this mistake and report a validation error, rather than allowing the server to report a protocol error. This limitation seemed obvious to me, but if you think extra validation would be helpful, feel free to open an issue requesting it (and assign to me).

Otherwise, it's unclear to me what headers would ever be useful here:

    const batchOpts: IODataBatchOptions = {
        webUrl : webAbsoluteUrl
        //Need a way here to override default options e.g Headers.
    };

It sounds like you want to specify ODataVersion: 3.0 here, but that would require us to maintain two parallel implementations of the batching algorithm.

octogonz avatar Sep 20 '16 23:09 octogonz

I am closing this bug. If you have more questions/clarifications, please reopen it. Thanks!

octogonz avatar Sep 20 '16 23:09 octogonz

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 30 '20 12:01 msft-github-bot