1

So this issue has already been noticed here:
https://github.com/joomla/joomla-cms/issues/39332

My component gets accessed via the API by a big company who is using the same access with hundreds of partners all over the world.

What they do is sending only this as header:

Cache-Control: no-cache
Content-Type: application/json

It works with all partners but Joomlas route function is missing the HTTP_ACCEPT in the header and so $mediaType is empty and throwing a:

"Could not match accept header"

What can be done to have a successful API access without the partner having to send a accept header?

Update 16.01.2024

I also tried accepting different headers, but seams i cannot handle an missing accept header. This in the route creation of my webservice plugin:

public function onBeforeApiRoute(&$router)
{
    $defaults    = [
        'component'  => 'com_mycomp',
        'format' => [
            'application/ld+json',
            'application/json',
        ],
    ];
    $getDefaults = array_merge(['public' => false], $defaults);

    // not working
    // $app = Factory::getApplication();
    // $app->setHeader('Accept', '*/*');

    $routes = [
        new Route(['GET'], 'v1/mycomp/time', 'mycomp.displayItem', [], array_merge(['endpoint' => 'time'], $getDefaults)),

It is described here:
https://github.com/joomla/joomla-cms/issues/39495

The getBest Function of the Negotiator requires a header:

if (!$header) {
   throw new InvalidArgument('The header string should not be empty.');
}

2 Answers 2

0

I think your content-type is wrong

"Content-Type": "application/vnd.api+json",
0

The header needs to be set on the server request ("input") and not on the application's response:

$app->getInput()->server->def('HTTP_ACCEPT', '*/*');

Joomla\Input\Input::def() is used here so it doesn't overwrite existing headers.

2
  • Where should this be put in?
    – maidan
    Commented Oct 10 at 10:25
  • In a plugin. onBeforeApiRoute event is fine. Perhaps onAfterInitialise would be better.
    – Sharky
    Commented Oct 11 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.