Search code examples
phpcurlartifactory

How to create/update key/value pair in JFrog Artifactory with PHP and cURL?


I am using PHP 8.2 to read our JFrog Artifactory API and this works fine. Now I am in the need to either create or update some of the properties on an artifact - so either create it if it does not exists, and update it if it does exists.

For example I can read all properties for a specific artifactory with this code:

<?PHP

// The full URL for the specific artifact and its "properties"
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties";

$ch = curl_init($api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "X-JFrog-Art-Api: ". $artifactoryKey,
));

// Execute the request to retrieve existing properties
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

?>

This will dump something like this:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

Now I want to create a new key/value pair inside the properties. For example if I want to create a new key named MyKey with a value of False then I would like to see this result:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ]
        "MyKey" : [ "False" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

I have of course tried various solutions like:

// Define the new key/value pair
$newProperty = array(
    'key' => "MyKey",
    'value' => "False"
);

// Prepare the JSON payload
$payload = json_encode(array("properties" => array($newProperty)));

// Initialize cURL to update the properties
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-JFrog-Art-Api: ' . $artifactoryKey
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Execute the request to update the properties
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

... and various tweaks to this but I cannot get it to work. Doing the above solution will give me this error:

string(98) "{
    "errors" : [ {
        "status" : 400,
        "message" : "Properties value cannot be empty."
    } ]
}"

My account should have anough access to write/modify (I am not the admin but have been told so) but the error message does not look like it is related to permissions either, so any help on this would be appreciated - I expect this may be a simple fix as I have not much experience with handling Artifactory stuff :-)


Solution

  • Actually I did figure out this myself and it was a stupid/simple mistake. I easily solved this after finding the right help page - I should probably have invested more time in finding this in the first place :-)

    Let me show the solution first:

    <?PHP
    
    // Full URL for specific artifact and the property to create/update
    $api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False";
    
    // Initialize cURL to create/update the properties
    $ch = curl_init($api);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "X-JFrog-Art-Api: ". $artifactoryKey
    ));
    
    // Execute the cURL request
    $response = curl_exec($ch);
    
    // Dump the result
    echo "<pre>";
    var_dump($response);
    
    // Check if the request was successful
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    var_dump($httpCode);
    
    // Close the cURL session
    curl_close($ch);
    
    ?>
    

    This code will result in a HTTP Status 204 (No Content) which equals a success and it will create the new propery named MyKey with the value of False. The cURL PUT command will work for both creating it or updating the value.

    Found the help for it here, https://jfrog.com/help/r/jfrog-rest-apis/set-item-properties