admin icon indicating copy to clipboard operation
admin copied to clipboard

Authentication breaks PATCH requests

Open xrogers opened this issue 1 year ago • 1 comments

API Platform version(s) affected: 4.0.11

Description
Authentication based on the example from docs https://api-platform.com/docs/admin/authentication-support/ leads to the weird side effect - PATCH requests fail with error stating that The content-type "application/ld+json" is not supported. Supported MIME types are "application/merge-patch+json".' Everything else works reasonably good, only PATCH is failing.

How to reproduce
Implement dataProvider accordingly to the docs. Try to update entity via admin.

xrogers avatar Dec 12 '24 20:12 xrogers

Looking at a similar issue (https://github.com/api-platform/admin/issues/578) made me wonder if this could be caused by this code:

const getHeaders = () =>
  localStorage.getItem("token")
    ? { Authorization: `Bearer ${localStorage.getItem("token")}` }
    : {};

const fetchHydra = (url, options = {}) =>
  baseFetchHydra(url, {
    ...options,
    headers: getHeaders,
  });

Indeed this seems to override any headers that could be present in options, so it could loose the content-type header in the process.

Can you try to merge the headers instead to see if this solves your issue?

const fetchHydra = (url, options = {}) =>
  baseFetchHydra(url, {
    ...options,
    headers: () => ({ ...options.headers(), ...getHeaders() }),
  });

slax57 avatar May 12 '25 16:05 slax57