Drupal7 REST:我无法执行POST和PUT方法。错误是:不可接受:节点类型是必需的,代码:406?

时间:2014-11-05 08:16:14

标签: rest http drupal drupal-7 httprequest

我正在使用drupal7。我的drupal_http_request()for get和delete对经过身份验证的用户工作正常,但post和put方法不起作用。

错误为:Not Acceptable : Node type is required,http错误代码为:406。我的代码如下:

function ws_form_post_auth() {

$base_url = 'http://localhost/drupalws/api/v1';
$data = array(
  'username' => 'student1',
  'password' => 'welcome',
);
$data = http_build_query($data, '', '&');
$options = array(
  'headers' => array(
    'Accept' => 'application/json',
  ),
  'method' => 'POST',
  'data' => $data
);

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

// Check if login was successful
if ($response->code == 200) {

    $options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
    $options['headers']['X-CSRF-Token'] = $data->token;

    $data = array(
        'title' => 'First forum post',
        'type'=> 'forum',
        'body'=> array(
            'und'=>array(
                0=> array(
                    'value'=>'This is my first forum post via httprequest.'
                )
            )
        )
    );
  $data = json_encode($data);

  $options['data'] = $data;
  $options['method'] = 'POST';

  $response = drupal_http_request($base_url . '/node', $options);

  return $response->status_message;
}
return $response->status_message;
}

1 个答案:

答案 0 :(得分:1)

我为我的问题找到了解决方案,我错过了标题中的内容类型。

[...] if($ response-> code == 200){

$options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;
$options['headers']['X-CSRF-Token'] = $data->token;
$options['headers']['Content-Type'] = 'application/json';

[...]