1

I am building an SPFX web part and trying to get data from a list using REST. Don't want all that noise returned so trying to use odata=nometadata.

I've read this and this and plenty of other sites and was made aware that Accept header will not work without specifying odata value. The issue I am having is that my custom headers work with Postman, but not in code.

The following request works in Postman (don't mind the Content-Type, that was just a matter of testing): enter image description here

Data is returned as expected. I've tried that will all odata values (verbose, minimalmetadata, and nometadata)

I am trying to use the same Accept header in code but it doesn't seem to be working:

const userResponses: Promise<SPHttpClientResponse>[] = [];  
const opt: ISPHttpClientBatchOptions = { headers: { 'Accept':'application/json;odata=minimalmetadata;'} }; 
const getUsers: Promise<SPHttpClientResponse> = spBatch.get(`${this._currentWebUrl}/_api/web/Lists/GetByTitle('${encodeURIComponent(list)}')/items?$select=User/Id,User/Title,User/Name,User/EMail,Manager/Title&$expand=User,Manager`,SPHttpClientBatch.configurations.v1, opt);  
userResponses.push(getUsers);

And this is what I get: enter image description here

Does anyone know what could be the issue here?

3

SharePoint Framework's http client internally uses the OData JSON format version 4.

So, in case of SPFx you need to modify it to use the format as odata.metadata=minimal.

In case you want verbose mode, change it as odata.metadata=full and if you only want the data and no extra metadata change it odata.metadata=none

Modify your code as below:

const userResponses: Promise<SPHttpClientResponse>[] = [];  
const opt: ISPHttpClientBatchOptions = { 
        headers: { 'Accept':'application/json;odata.metadata=minimal'} 
    }; 
const getUsers: Promise<SPHttpClientResponse> = spBatch.get(`${this._currentWebUrl}/_api/web/Lists/GetByTitle('${encodeURIComponent(list)}')/items?$select=User/Id,User/Title,User/Name,User/EMail,Manager/Title&$expand=User,Manager`,SPHttpClientBatch.configurations.v1, opt);  
userResponses.push(getUsers);

Having said that, if you want to keep using OData format 3, then you need to explicitly specify it as below by leaving it blank:

const userResponses: Promise<SPHttpClientResponse>[] = [];  
const opt: ISPHttpClientBatchOptions = { 
        headers: { 
            'Accept':'application/json;odata=nometadata',
            'odata-version': ''
        } 
    }; 
const getUsers: Promise<SPHttpClientResponse> = spBatch.get(`${this._currentWebUrl}/_api/web/Lists/GetByTitle('${encodeURIComponent(list)}')/items?$select=User/Id,User/Title,User/Name,User/EMail,Manager/Title&$expand=User,Manager`,SPHttpClientBatch.configurations.v1, opt);  
userResponses.push(getUsers);
  • Thanks. odata.metadata=none works fine. I couldn't get it to work using V3. After RTFM I tried using 'DataServiceVersion':'3.0', 'odata-version': '', and 'odata-version': '3.0', but without any luck. – Paul Strupeikis Oct 30 '18 at 10:58
  • True that, looks like a buggy client. Any way SPHttpBatchClient is in preview, so looks like this expected. Would suggest that you open an issue in the issue list - github.com/SharePoint/sp-dev-docs/issues and hope MS picks it up and fixes it – Gautam Sheth Oct 30 '18 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.