I've read this and this but nothing is working for me I'd like to create a menu like this

$items['login'] = array(
'page callback' => 'mymodule_login',
'access arguments' => array('access content'),
'access callback' => true,
'type' => MENU_CALLBACK,

in order to send a POST to http://www.mysite.com/login.

This is my script in order to generate a POST


define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

$base_url = 'http://www.mysite.com';
$data = array(
  'username' => 'admin',
  'password' => 'password',
$data = http_build_query($data, '', '&');
$headers = array();
$options = array(
  'headers' => array(
    'Accept' => 'application/json',
  'method' => 'POST',
  'data' => $data

$response = drupal_http_request($base_url . '/login', $options);
$data = json_decode($response->data);

And this is my callback function

function mymodule_login() {
  header('Content-type: application/json');
  watchdog('mymodule',  print_r($_POST, TRUE));
  $result = array("result" => "ok");
  echo drupal_json_output($result);

The body contains a json with username and password. Everything is ok, except for $_POST under function mymodule_login that does not work. $_POST is empty.

What's wrong? Is there another "drupalic" way?

  • Does the body contain only JSON or URL encoded key-value pair? The request body should contain something like: json=[url encoded json string] and you will then be able to access it via $_POST['json']. Posting the code for mymodule_login would help. – hw. Jun 6 '13 at 15:40
  • I updated my request – mrta75 Jun 6 '13 at 16:12
  • Your code (4th revision) looks correct. Did you clear cache ? Show us a var dump of $response – AyeshK Jun 6 '13 at 16:57
  • Yes clear all via drush everytime. watchdog('citynotifier', print_r($response, TRUE)); gave "Undefined variable: response in citynotifier_login()" – mrta75 Jun 6 '13 at 20:36
  • 1
    Here's the solution Content type "application/json" disable $_POST so you must use file_get_contents("php://input") – mrta75 Jun 7 '13 at 11:20

Your Answer

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

Browse other questions tagged or ask your own question.