发布请求来自drupal 8(Guzzle)

时间:2016-08-11 09:42:29

标签: http post drupal guzzle

我正在尝试从Drupal发送一个帖子请求到Redmine Api发送一个json。 但是当我尝试它时,我收到了一个错误。

要发送的数据:

$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

我已经研究了错误,看起来标题中的内容类型存在问题。但是,如果我尝试发送没有标题选项的请求,则会出现与验证相关的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

任何人都有任何暗示吗?

我很挣扎。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

嗯,首先,我不建议手动输入JSON并尝试转换它。其次,看起来你无缘无故地转换JSON字符串。只需发送字符串本身。格式将以您发送的方式无效。希望这会有所帮助。