1

When I read this HTTP Client Manager docs on Drupal, I don't understand why it returns the $build when $request->getStatusCode() == 200, is that supposed to be not equal 200? I also copy and paste the code from Drupal docs.

public function posts($limit, $sort) {
    $build = [
      '#theme' => 'mymodule_posts_list',
      '#posts' => [],
    ];

    $request = $this->httpClient->request('GET', 'http://api.example.com/posts', [
      'limit' => $limit,
      'sort' => $sort,
    ]);

    // This is the Part I don't understand.
    if ($request->getStatusCode() == 200) {
      return $build;
    }

    $posts = $request->getBody()->getContents();
    foreach ($posts as $post) {
      $build['#posts'][] = [
        'id' => $post['id'],
        'title' => $post['title'],
        'text' => $post['text'],
      ];
    }
    return $build;
  }
2
  • 1
    I suspect that the condition here should be != 200... e.g. return an empty array of posts if the api does not get a valid response.
    – CG Monroe
    Jun 14, 2019 at 16:18
  • Thank you, CG Monroe
    – Jiang Zhan
    Jun 14, 2019 at 16:54

1 Answer 1

0

The information from https://www.drupal.org/docs/contributed-modules/http-client-manager/introduction was wrong and was updated, now:

   if ($request->getStatusCode() != 200) {
      return $build;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.