I have an Titanium Android app that displays images from URLs. I would like to use apigilty to serve these images.
I have a stream response RPC service which works when called from a browser. However on android it does not work as it appears the android ImageView
does not send a Accept-Type
header and Apigiilty rejects the call with a 406 - Cannot honor Accept type specified
.
Is there a way I can configure my apigility project to ignore the Accept-Type
check just for this RPC service?
The Accept
header of the incoming request is checked inside the ZF\ContentNegotiationAcceptFilterListener
class and if no accept header is present the validateMediaType
method returns false
and then the ApiProblemResponse
is created at line 55.
The best would of course be to make sure that you get an accept header inside all your requests, but in case this is somehow impossible you could set a default accept header inside all your requests.
You can simply solve it by adding this in your application Module.php
method:
/**
* {@inheritDoc}
*/
public function onBootstrap(MvcEvent $event)
{
$application = $event>getApplication();
$eventManager = $application->getEventManager();
// Attach setDefaultAcceptHeader method with higher priority then AcceptFilterListener
$eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'setDefaultAcceptHeader'], 100);
}
/**
* @var MvcEvent $event
*/
public function setDefaultAcceptHeader(MvcEvent $event)
{
$request = $event->getRequest();
if( ! $request instanceof \Zend\Http\Request){
// Not a http request, return
return;
}
// Check if we have an accept header, if not set to 'application/json'
$headers = $request->getHeaders();
if(!$headers->has('accept')){
$headers->addHeaderLine('accept', 'application/json');
}
}