Skip to content

Authentication breaks PATCH requests #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
xrogers opened this issue Dec 12, 2024 · 1 comment
Open

Authentication breaks PATCH requests #590

xrogers opened this issue Dec 12, 2024 · 1 comment

Comments

@xrogers
Copy link
Contributor

xrogers commented Dec 12, 2024

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.

@slax57
Copy link
Contributor

slax57 commented May 12, 2025

Looking at a similar issue (#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() }),
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants