phpazurecurlazure-stackhttp-status-code-411

Azure Stack API Rest create storage account with CURL in PHP


I try to create a storage account in my Azure Stack with curl in a PHP site but unfortunately I always receive a error "HTTP Error 411. The request must be chunked or have a content length.".

I have put ""Content-Length: 0,"" or the correct number but still same error.

Here is my code (who is generated by Postman) :

    $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://management.myazurestack.com/subscriptions/XXXXXX/resourcegroups/YYYYYYYY/providers/Microsoft.Storage/storageAccounts/sa01?api-version=2017-10-01",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_SSL_VERIFYPEER => 0, 
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "{\r\n                \"sku\": {\r\n                  \"name\": \"Standard_LRS\"\r\n                },\r\n                \"location\": \"myregion\"\r\n              }",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Authorization: ". $fBearer,
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 0",
    "Content-Type: application/json",
    "Host: management.myazurestack.com",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Any idea ?

Thanks


Solution

  • You have to use either Content-Length or chunking, but not both.

    This is how you use Content-Length:

    Content-Length: 1024
    

    And this is how you use Transfer-Encoding:

    Transfer-Encoding: chunked
    

    There is an Azure SDK for PHP which makes things much easier to manage.