0

I created a custom path via hook_menu with a page callback to a function that prints json.

It all works in the sense that requesting: http://cms.tripchi.com/tripchi/getme returns my JSON, but the header says it's 404 which is throwing off everything in the app. This only returns the 404 status on the client's godaddy server (PHP Version 5.4.19 w/ CGI/FastCGI), but returns 200/Ok on my local PHP Version 5.3.5 w/ Apache 2.0 Handler. All Drupal configurations and databases are equal.

hook_menu

function tripchi_menu() {
    return array(
        'tripchi/getme' => array(
            'title' => 'My Profile',
            'description' => 'mobile test for user status and object',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'page callback' => 'tripchi_getme')
    );
}

callback function

function tripchi_getme(){
    $user = $GLOBALS['user'];
    $json = array();
    // ... a bunch of stuff to construct a special array;
    drupal_add_http_header('Status', 'Ok');
    drupal_add_http_header('Access-Control-Allow-Origin', '*');
    http_response_code(200);
    //echo json_encode($json);
    drupal_json_output($json);
    drupal_exit();
    //die();
    // i've tried all combinations of commenting/uncomming the above...
}

below screenshot of the requests locally (right) and remotely (left): below screenshot of the requests locally (right) and remotely (left)

why does this always return 404 but also give me my perfectly json encoded data?

1
  • i rebuilt the whole Block into a Service and i'm getting my data with a 200/Ok code. the link above will now show 200, but still have not figured out what was wrong but right now i'm blaming the Apache/PHP configure over Drupal.
    – E.A.T
    Commented Aug 20, 2014 at 11:02

4 Answers 4

1

First of all, for JSON output use drupal_json_output as a delivery callback in your menu. It will make thinks much easier:

function tripchi_menu() {
    return array(
        'tripchi/getme' => array(
            'title' => 'My Profile',
            'description' => 'mobile test for user status and object',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'page callback' => 'tripchi_getme',
            'delivery callback' => 'drupal_json_output'
         ),
    );
}

Then in your menu callback just return array. You don't need any other fancy stuff. I also faced 404 issue, but with this solution AND explicitly set response code 200 everything worked fine.

function tripchi_getme(){
    global $user;
    $json = array();
    // ... a bunch of stuff to construct a special array;

    // Only this was needed to make the stuff work.
    http_response_code(200);

    return $json;
}

I couldn't tell if the cause of my trouble was drupal or apache or both, but this certainly helped. Note that Access-Control-Allow-Origin was not needed to make this work (and I didn't plan to use cross domain calls either).

3
  • i was really hoping this was the answer but it did not make a difference for me. I tried the 'delivery callback' with all varieties of returning $json and drupal_json_output($json) with and without explicit headers like http_resposne_code(200). All return a 404 response code, but either proper json or html with the error Fatal error: Cannot use object of type stdClass as array in /home/xxxx/includes/common.inc on line 5836
    – E.A.T
    Commented Aug 31, 2014 at 8:38
  • @EliATaylor The error you quoted suggests that your menu callback was returning an object instead of array. Commented Aug 31, 2014 at 15:34
  • i just double-backed on this and found this answer was correct. Thanks!
    – E.A.T
    Commented Feb 16, 2015 at 22:00
0

Tried code below. Works fine. Did you check if the user you're using has 'access content' permission?

... hook_menu ...
  $items['tripchi/getme'] = array(
    'title' => 'My Profile',
    'access arguments' => array('access content'),
    'page callback' => 'tripchi_getme',
  );

return $items; }

function tripchi_getme(){ $json = array(); // ... a bunch of stuff to construct a special array; drupal_add_http_header('Access-Control-Allow-Origin', '*'); drupal_json_output($json); }

enter image description here enter image description here

1
  • thanks @rostislav but yes, i've tried with and without the permissions. i've updated my question with a similar photo of requests from both my local and remote machine. plus shouldn't a permissions issue cause a different code and not return my data?
    – E.A.T
    Commented Aug 19, 2014 at 19:15
0

for the record, i ended up giving up on Blocks and moved this whole function under Services which already replies in JSON and seems to handle access / reponse codes properly.

0

I was having the same error as you, getting "status code 404" and the problem was that I have in the project of Drupal in root directory one file that start the same as my API, example:

Root directory:

/var/www/drupal_proyect.com/

my file in that directory that was causing that error:

api_x.apk

and I have a custom API called: api_x

hook_menu

function api_x_menu() {
    return array(
        'api_x/test' => array(
            'title' => 'My Profile',
            'description' => 'mobile test for user status and object',
            'access callback' => 'user_access',
            'access arguments' => array('access content'),
            'page callback' => 'api_x_test')
    );
}

callback function

function api_x_test(){
    echo "ok";
}

The solutions

  1. change name of the file in the root directory
  2. or add http_response_code(200); before echo "ok";
  3. or add header( 'HTTP/1.2 200 OK' ); before echo "ok";

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.