Forum Discussion
Create Attachment in Azure Project is not working
1 Reply
- haneefvfCopper Contributor
Solved code Sample
$data = fopen("test.png", 'rb');
$size = filesize($file);
$contents = fread($data, $size);
fclose($data);
$url = "https://dev.azure.com/{OrganizationName}/{ProjectId}/_apis/wit/attachments?fileName=test.png&uploadType=simple&api-version=6.0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$header = array("Authorization:Basic ".base64_encode($params['username'].":".$params['token']),"Content-Type:application/octet-stream");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contents);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
Link with workitem
$postRequest = [];
$postRequest[] = ["op"=>"add",
"path"=> ("/relations/-"),
'from' => null,
"value"=> ["rel"=>"AttachedFile",
"url"=>$response->url
]
];
$url = "https://dev.azure.com/{OrganizationName}/{ProjectId}/_apis/wit/workitems/{workitemId}?api-version=6.0";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$header = array("Authorization:Basic ".base64_encode($params['username'].":".$params['token']),"Content-Type:application/json-patch+json");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postRequest));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);