K4 Panel API redirects

Hey there!
I have had some problems with the K4 panel api and I am not sure if I do something wrong or if it’s a bug.

In some of my plugins I used this.$api to call custom endpoints. I switched that to panel.api for example:

panel.api
    .post('komments/verify', {
        pageSlug: pageSlug,
        kommentId: kommentId,
        isVerified: isVerified,
    })

In the panel you click a button to trigger the call. The following happens:

  • You click the button
  • The async call is triggered
  • You immediately get redirected to the API url and see a 401 json result

After clicking the button I can see the changes in the markdown file, so the code of the endpoint seems to work. But whatever I do the redirect follows.

It does not matter if it’s a GET or POST call I will be redirected. In this example to:
https://DEVHOSTNAME/api/komments/verify


Okay… so while I am writing this I tried to write a simplified example of the endpoint and figured out, what the problem is!

Until now, the api endpoint ended with a return statement like so:

return json_encode(['message' => 'okay']);

This will end up in that redirect.

Changing it to:

return new Response(json_encode(['message' => 'okay']), 'application/json');

The call is then executed and no redirect follows.

So all the endpoint where I have older code without the Response end with a redirect, the others work.
I will post this anyway, maybe somebody else is stumbling upon the problem and finds this solution.

Also this may be a bug? As long as I return something I wouldn’t expect the redirect behavior.

1 Like

Ping @distantnative

I have occasionally after errors during development encountered a similar thing. So I think somewhere something is a bit off and those redirects shouldn’t really happen (so a little bug). Would love to look closer into this once 4.0 is out.