Http drupal 8的Post请求(Guzzle)
我正在尝试从Drupal向RedmineAPI发送post请求,以发送json。 但我尝试时出错了 要发送的数据:Http drupal 8的Post请求(Guzzle),http,post,drupal,guzzle,Http,Post,Drupal,Guzzle,我正在尝试从Drupal向RedmineAPI发送post请求,以发送json。 但我尝试时出错了 要发送的数据: $rawdata = '{ "issue": { "project_id": 85, "subject": "Test from drupal", "priority_id": 4 } }'; $data = json_encode($rawdata); 后请求: $request = \Drupal::httpClient
$rawdata = '{
"issue": {
"project_id": 85,
"subject": "Test from drupal",
"priority_id": 4
}
}';
$data = json_encode($rawdata);
后请求:
$request = \Drupal::httpClient()->post($url,array(
'headers' => array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'X-Redmine-API-Key' => $this->apiKey),
));
$request->setBody($data);
$response = $request->send($request);
这就是出现的错误:
[:error] [pid 8316] [client XXX.XXX.XXX.XXX] FastCGI: server "/usr/lib/cgi-bin/php5-fcgi_xxxx" stderr: PHP message: Uncaught PHP Exception GuzzleHttp\\Exception\\ClientException: "Client error: 422" at dir/to/project/vendor/guzzlehttp/guzzle/src/Middleware.php line 69, referer: http://url-from-project/create
我已经对错误进行了研究,似乎标题中的内容类型存在问题。但是,如果我尝试在不使用headers选项的情况下发送请求,则会出现与身份验证相关的401客户端错误
是这样的:
$request = \Drupal::httpClient()->post($url,array(
'Accept' => 'application/json',
'Content-type' => 'application/json',
'X-Redmine-API-Key' => $this->apiKey,
));
它抛出了这个错误:
[:error] [pid 8159] [client XXX.XXX.XXX.XXX] FastCGI: server "/usr/lib/cgi-bin/php5-fcgi_xxxx" stderr: PHP message: Uncaught PHP Exception GuzzleHttp\\Exception\\ClientException: "Client error: 401" at /dir/to/project/vendor/guzzlehttp/guzzle/src/Middleware.php line 69, referer: http://website.com/create
有人有任何暗示吗
我为此而挣扎。任何帮助都将不胜感激。首先,我不建议手动键入JSON并尝试转换它。其次,看起来您正在毫无理由地转换JSON字符串。只需发送字符串本身。您发送格式的方式将无效。希望这有帮助。首先,我不建议手动键入JSON并尝试转换它。其次,看起来您正在毫无理由地转换JSON字符串。只需发送字符串本身。您发送格式的方式将无效。希望这有帮助