How to send POST request to https using \Drupal::httpClient()? I want to send POST request to https using \Drupal::httpClient().
But ended up with error like


  GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate
  problem: self signed certificate (see
  http://curl.haxx.se/libcurl/c/libcurl-errors.html)


Below is my code.

$post_fields['name'] = 'aa';
    $fields_string = json_encode($post_fields);    
    $response = \Drupal::httpClient()
           -&gt;post("https://localhost/example/api/user-update?_format=json", [
         'auth' =&gt; ['admin', 'admin'],
         'body' =&gt; $fields_string,
    'http_errors' =&gt; FALSE,
         'headers' =&gt; [
           'Content-Type' =&gt; 'application/json',
    ],
       ]);
    dsm($response-&gt;getBody()-&gt;getContents());


How to send POST request with \Drupal::httpClient().
The error message should inform you what is going on. You are using a self-signed certificate locally. This probably won't be an issue on your production sites, but this is not really useful for local development.

Inspect the \Drupal::httpClient() static method you may discover that it uses the http_client_factory:fromOptions service. Use this directly allows you to pass options into Guzzle to allow requests to sites using self-signed certificates.

$clientFactory = \Drupal::service('http_client_factory');
$client = $clientFactory-&gt;fromOptions(['verify' =&gt; FALSE]);


Obviously this is not ideal. You should probably parameterize the URL that you are using instead of hard coding the URL. Then locally you can test without a self-signed cert or HTTPS. Or parameterize the options you pass into Guzzle so that verify =&gt; TRUE when you're on production. It is not recommended to set it to FALSE and this is a security risk.