Home > Blockchain >  Calling Azure Release Create with PHP
Calling Azure Release Create with PHP

Time:11-11

I'm trying to use pure PHP to call the Azure DevOps API REST to create a release pipeline. The json required for the body is working with POSTMAN but PHP returns this error:

Array ( [$id] => 1 [innerException] => [message] => VS402903: The specified value is not convertible to type ReleaseStartMetadata. Make sure it is convertible to type ReleaseStartMetadata and try again. [typeName] => Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data [typeKey] => InvalidRequestException [errorCode] => 0 [eventId] => 3000 )

It does not make any sense, but I think there is something regarding the return of the information instead of the information sent through the body.

Here's my code:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$pat='hidden';

$url = 'https://vsrm.dev.azure.com/org-id/workspace-name/_apis/release/releases?api-version=6.0';

$body = '{
  "definitionId": 1,
  "description": "test",
  "name": "test",
  "isDraft": false,
  "reason": "manual"
}';

$data_json = json_encode($body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8; api-version=6.0','Authorization: Basic '.$pat));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
$data = json_decode($response, true);
print_r ($data);
curl_close($ch);
?>

CodePudding user response:

I found the answer, the error is because the json that I was sending through the body has some escaping characters \n\ etc.

With this, I have cleaned it up, and put it below the $data_json = json_encode($body);

$body_formatted=rtrim(ltrim(str_replace('\\', '', str_replace('\n', '', $data_json)), '"'), '"');

Use body_formatted on the post option curl_setopt($ch, CURLOPT_POSTFIELDS,$body_formatted);

Hope it's helps for anyone.

  • Related