I'm going off this Guzzle document:
https://www.drupal.org/docs/8/core/modules/rest/3-post-for-creating-cont...

General Questions, Does "Drupal::httpClient()" use curl by default (as opposed to guzzle) in the sample below?
With "use GuzzleHttp\ClientInterface;", does this override which httpClient gets called, so we get guzzle features in httpClient?

I'm only after the X-CSRF-Token method, to start with. The "'auth' => ['klausi', 'secret']" example kind of makes me squirm. Setting up a secure token, against an account with proper permissions should prob get documented/linked along with this.

<?php
$serialized_entity = json_encode([
  'title' => [['value' => 'Example node title']],
  'type' => [['target_id' => 'article']],
  '_links' => ['type' => [
      'href' => 'http://example.com/rest/type/node/article'
  ]],
]);

$response = \Drupal::httpClient()
  ->post('http://example.com/entity/node?_format=hal_json', [
    'auth' => ['klausi', 'secret'],
    'body' => $serialized_entity,
    'headers' => [
      'Content-Type' => 'application/hal+json',
      'X-CSRF-Token' => <obtained from /rest/session/token>
    ],
  ]);
?>

Comments

sgroundwater created an issue.

sgroundwater’s picture

Note, pls ignore my confusion above, think I answered myself, guzzle uses Curl as it's handler.

See: ~/scripts/guzzle/src/Handler$ ls
CurlFactoryInterface.php CurlFactory.php CurlHandler.php CurlMultiHandler.php EasyHandle.php MockHandler.php Proxy.php StreamHandler.php

sgroundwater’s picture

As I'm working through this issue, I found this link helpful.
https://drupal.stackexchange.com/questions/248772/create-a-node-using-th...

Just wanted to drop this here for now.

Status Update: I have things working in my dev, and hope to have a patch/solution soon.

sgroundwater’s picture

While looking at how to add Basic Authentication here, i ended up reworking this module beyond the simple patch I was planning to drop.

Here's a sandbox module, using Basic Auth, that also includes sample content types for testing. While I used "Rulles HTTP Client" to get me started, the sandbox code is a bit different in how it goes about things under the hood. I've only focused on POST with Basic Auth, because this is what I need most at the moment.

https://www.drupal.org/sandbox/sgroundwater/3037075

I haven't given up on a patch for this issue, I'm just figuring out things on the side for now.