Home
About
Search
Welcome to M-Files Empower – our new support experience. We'd love to hear what you think!Give feedback
Home/Support and troubleshooting/Examples and how to

REST API used from JavaScript in another site is missing cookies

Last updated on 11 December 2024 · 6 min read

Admin
REST API

Overview

When using the M-Files REST API in JavaScript in another site (such as a SharePoint web part, or the front-end of some custom web application), you may be unable to "see" the necessary cookies and thus send them back to the M-Files Server.

This may also manifest as unexpected 403 responses that may even appear somewhat random - some requests may succeed (roughly 50%) if they are received by the same server node that issued the token (more on these principles in the linked documentation below).

Details

When connecting to the M-Files REST API from the front-end of your web application, you need to take several things into account:

  • This is a CORS request so you must add the domain to the allowed CORS domains of your vault.
    • This is the reason why you cannot access cookies with your code.
    • There are various more requirements and restrictions browser impose around CORS requests that are beyond the scope of this article and M-Files support.
    • This is not the case when this is some other application or a backend, this applies to web pages running inside a browser.
  • The cookies sent by M-Files are HTTP only, so you cannot access them with JavaScript, they are only available to the HTTP request of the browser. 
    • This is a security feature of the browser that cannot be circumvented.
  • Authentication to the REST API does not happen via cookies; you only need them for correct load-balancing in the M-Files Cloud. 
  • Your browser (user agent) must accept third party cookies on the domain you use for your current site (not the vault). Otherwise, the load-balancing cookie sent by the M-Files Cloud server may not get stored by the browser, and then not sent on subsequent requests. If that happens, usually the browser dev tools should show a warning in the Set-Cookie response header.
    • For example, if you are developing on localhost and access the test site as http://1.2.3.4:5678, you need to have that address added to the CORS settings of the vault and also to your browser settings to allow third party cookies (at the time of writing, called "sites allowed to use third party cookies" in Chrome).
    • By default, Chromium-based browsers block third party cookies at the time of writing.
    • Note that this applies to the end users' machines that will ultimately will be making those requests to the vault - they must also accept third party cookies on the site that consumes the vault REST API.

Solution

When making the request from inside your own site, both for the authentication token and also for the subsequent data requests, use the corresponding browser API:

  • For XHR requests, the withCredentials property.
  • For Fetch API, the credentials: include and mode: cors options.

This tells the browser to keep and send the cookies from and to the other domain (the M-Files REST API domain in this case).

You can read more about these browser features in online resources such as these:

Note that these are third party sites and M-Files cannot guarantee their availability and content.

Make sure you understand the implications of these security relaxations and that this does not put your own application at risk (e.g., by sending/storing insecure cookies or credentials). Such analysis is not in the scope of support of M-Files.

You also need to be running M-Files Server 24.2 or later (February 2024 release) so that the access-control-allow-credentials header is sent by the REST API, which allows the CORS requests with the settings above.

Examples

Note that these examples are for illustrative purposes only, to showcase the necessary CORS settings. They are not examples of anything else - such as good quality code, storing credentials properly, handling errors well, async-await patterns. Scripting and coding are not covered by the standard M-Files Support policy. You may need to adapt such examples to your codebase.

XHR request

function submitRequest() { var xhr = new XMLHttpRequest(); xhr.open( "POST", "https://yourVault.cloudvault.m-files.com/REST/server/authenticationtokens", true ); xhr.setRequestHeader( "Accept", "*.*" ); xhr.setRequestHeader( "Content-Type", "application/json" ); xhr.withCredentials = true; xhr.onreadystatechange = function(){ if (xhr.readyState == XMLHttpRequest.DONE && this.status == 200 ) { var token = JSON.parse( xhr.responseText ).Value; submitRequest2(token); } } var bodyData = '{"Username":"yourUser","Password":"yourPassword","VaultGuid":"{B8B336DA-9EAD-4152-AFB2-1ED843CD800A}"}'; xhr.send(bodyData); } function submitRequest2(token) { var xhr = new XMLHttpRequest(); xhr.open( "GET", "https://yourVault.cloudvault.m-files.com/REST/objects/0/542/latest", true ); xhr.setRequestHeader( "X-Authentication", token ); xhr.onreadystatechange = function(){ if (xhr.readyState == XMLHttpRequest.DONE && this.status == 200 ) { alert("yey"); } } xhr.withCredentials = true; xhr.send(); } submitRequest();

Fetch API

async function submitRequest() { const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); myHeaders.append("Accept", "*.*"); const myRequest = new Request("https://yourVault.cloudvault.m-files.com/REST/server/authenticationtokens", { method: "POST", body: JSON.stringify({{"Username":"yourUser","Password":"yourPassword","VaultGuid":"{B8B336DA-9EAD-4152-AFB2-1ED843CD800A}"}}), headers: myHeaders, credentials: "include", mode: "cors" }); const response = await fetch(myRequest); if (!response.ok) { throw new Error("something went wrong"); } const tokenResponse = await response.json(); const myHeaders2 = new Headers(); myHeaders2.append("X-Authentication", tokenResponse.Value); const myRequest2 = new Request("https://yourVault.cloudvault.m-files.com/REST/objects/0/542/latest", { method: "GET", headers: myHeaders2, credentials: "include", mode: "cors" }); const response2 = await fetch(myRequest2); if (!response.ok) { throw new Error("something went wrong"); } const objData = await response2.json(); console.log(objData); } submitRequest();

Still need help?