I have a hook_menu URL exposed as a URL and I am also calling the same callback function separately in code The URL returns 404 at error at sometimes and starts working fine after a cache clear. What could be the possible issue ? hook_menu Implementation

  $items['test_link/get-response'] = array(
    'title' => t('Get Response'),
    'page callback' => 'get_response',
    'page arguments' => null,
    'access callback'   => true, // available to all
    'type' => MENU_CALLBACK,
  ); // end

function get_response($funParams=array(),$res='json'){
  $allParams = drupal_get_query_parameters($_GET);  

  $result = array();  
  $result["status"] = TRUE;

  // Processing on allParams and builds the response array
  return displayJSONResponse($result,$res);

function displayJSONResponse($response,$res='json'){        
        return $response;
    $res = '';
    $res = json_encode($response, JSON_PRETTY_PRINT);  
    $res = json_encode($response);
    $json = str_replace('\\/', '/', $res);
    echo $json;    
  • Please include the hook_menu implementation: it's likely that there's an error in the way you have coded it. Mar 8, 2017 at 9:56
  • I have added the code in question description now. Please note that depending on the value on $res value (second parameter in callback function ) I decide whether to send the array in response or print the json data. I did this to minimize code re-writing
    – mjs
    Mar 9, 2017 at 2:54

1 Answer 1


Page arguments, if specified, should always be an array. If you do not want to pass any arguments to your callback you should either code an empty array or omit the parameter. From experience I am fairly confident that this is your problem.

Also, the correct way to return a JSON response is to return NULL from your callback after printing the JSON, rather than exiting. This ensures that cleanup tasks can be run.

Your code does not comply with Drupal standards, and I would strongly advise you to follow them. It's a major part of learning to work with the framework. https://www.drupal.org/docs/develop/standards

  • Thanks for these pointers. I will change these and revert with response from LIVE site. I have one doubt though. Why is 404 returned randomly ? The code flow works fine for most of the days and sometimes it suddenly starts giving 404. Which can be removed after cache clear. and then things start working fine again.
    – mjs
    Mar 10, 2017 at 6:21
  • 1
    It's because the menu hook is processed differently when it's being added to cache to when it's restored from it. It's an anomaly and it's confusing but that's how it is. Mar 10, 2017 at 10:28
  • Thanks .. I will update in case the issue still persists.. It has been running fine since quite sometime now.
    – mjs
    Mar 10, 2017 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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