gpt4 book ai didi

rest - Drupal7 REST : I am not able to perform POST and PUT methods. 错误是 :Not Acceptable : Node type is required, 代码 :406?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:29 25 4
gpt4 key购买 nike

我正在使用 drupal7。我用于获取和删除的 drupal_http_request() 对于经过身份验证的用户工作正常,但 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;
}

最佳答案

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

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

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

[....]

关于rest - Drupal7 REST : I am not able to perform POST and PUT methods. 错误是 :Not Acceptable : Node type is required, 代码 :406?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26752427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com