1

I am trying to make a call to sharepoint list using REST APIs. I am logged in to sharepoint and when in browser tab, I enter the rest url like:

https://myserver.sharepoint.com/sites/mylist/_api/lists/getbytitle('OEC_Docs')/items

It returns me the data in the browser as expected. However using the code given below in my angular 4 app throws 403 error:

getDocs(){ const httpOptions = { headers: new HttpHeaders({ 'Accept': 'application/json;odata=verbose', 'Content-Type': 'application/json' }) }; return this.http.get(this.sharePointRestHost, httpOptions) } 

As the app is running on localhost in the same browser. So I am expecting it to work.

Please advise how to fix it.

    1 Answer 1

    0

    You have to supply digest within the header.

    You will find the necessary value in the element called '__REQUESTDIGEST'. So with jQuery you can get the value like this: $('#__REQUESTDIGEST').val().

    This value needs to be added to the header:

    headers: { "Accept": "application/json;odata=verbose", "X-RequestDigest": $('#__REQUESTDIGEST').val() } 
    3
    • 1
      Why do you need the request digest in a GET call?CommentedNov 6, 2018 at 13:21
    • well then what is the way to make a call using a javascript/html (angular) app? i get a 404 if i use the app. directly pasting the url in browser just works.
      – Vik
      CommentedNov 15, 2018 at 0:47
    • A 404 error is strange. Are you sure you escaped the URL correctly? Browsers do that automatically and often display the un-escaped URL. For authentication from external apps I would recommend looking into sp-request (npm). You can do the authentication yourself, it isn't that complicated, but the library is very solid. I use it in conjunction with ADFS-authentication from a corporate network.
      – Anonymous
      CommentedNov 26, 2018 at 15:24

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.