Search code examples
microsoft-graph-apispfx

How to replace the Content-Type header in SPFx MS Graph request?


I am trying to update the manager of a user with MSGraphClient. However the Graph API adds in a wrong HTTP header.

So the code I am using is this:

  const directoryObject = {
    "@odata.id": `https://graph.microsoft.com/v1.0/users/[managerID]`
  };
  response = await graphClient.api(`/users/[userID]/manager/$ref`).version('beta').header('content-type', 'application/json').put(directoryObject);

The [managerID] and [userID] are the respective AD GUID IDs for the users in question.

Now the problem is that the MSGraphClient adds a Content-Type to the request so the final request has the Content-Type header of application/octet-stream, application/json and because of that I get the following error: Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.

I tested it out in Postman, the request URL and Payload where correct and if I only changed the Content-Type to application/json alone then there was no issue where if I used the application/octet-stream, application/json I got the same error. However I can't find anything on how to remove or replace the application/octet-stream.

I am using SPFx 1.12.1


Solution

  • So after trial and error I found out that you need to set the header as "Content-Type" i.e. in upper camel case and not in lower case as I originally had.

    So the correct code is:

    response = await graphClient.api(`/users/[userID]/manager/$ref`).version('beta').header('Content-Type', 'application/json').put(directoryObject);
    

    And this will completely replace the content type header and not append it to the header