Requette sur un API avec methode POST sur API de HARBOR

I’m trying to tag an artifact (container image) on harbor with curl using the harbor api v2.0.

I noticed that the tag must be passed on the body of the request, which is of type json.

enter image description here

this is my curl command:

  curl -v -k -X POST -L -u <username>:<password>\
    -H "Content-Type: application/json"\
    -d '{"tag": {"name": "0.0.1-snapshot"}}'\
    "<https://mon_url_harbor>/api/v2.0/projects/<project_name>/repositories/<repository_name>/artifacts/<reference>/tags"

This is my output of my curl command:

{“errors”:[{“code”:“UNPROCESSABLE_ENTITY”,“message”:“validation failure list:\ntag in body is required”}]}

Any help please!

The error message tag in body is required suggests that the Harbor API expects a different structure in the JSON body for tagging the artifact. Based on the Harbor API v2.0 documentation, the body should directly contain the name property instead of wrapping it inside a tag object.

Here’s how you can modify your curl command to make the request correctly:

Corrected curl Command

curl -v -k -X POST -L -u <username>:<password> \
    -H "Content-Type: application/json" \
    -d '{"name": "0.0.1-snapshot"}' \
    "https://<mon_url_harbor>/api/v2.0/projects/<project_name>/repositories/<repository_name>/artifacts/<reference>/tags"

Key Adjustments:

  1. The JSON body should be {"name": "0.0.1-snapshot"} instead of {"tag": {"name": "0.0.1-snapshot"}}.
  2. Replace <username>, <password>, <mon_url_harbor>, <project_name>, <repository_name>, and <reference> with the appropriate values:
  • <username> and <password>: Your Harbor credentials.
  • <mon_url_harbor>: Your Harbor instance URL.
  • <project_name>: The name of the project in Harbor.
  • <repository_name>: The name of the repository in Harbor.
  • <reference>: The digest or tag of the artifact to which you want to add the new tag.

Example

If your project name is my-project, repository name is my-repo, artifact reference is sha256:abcd1234, and you want to add the tag 0.0.1-snapshot, the command will look like:

curl -v -k -X POST -L -u admin:password \
    -H "Content-Type: application/json" \
    -d '{"name": "0.0.1-snapshot"}' \
    "https://my-harbor-instance/api/v2.0/projects/my-project/repositories/my-repo/artifacts/sha256:abcd1234/tags"

Output

If successful, the API will return a 201 Created response, indicating the tag was added to the artifact successfully.

Additional Tips

  • Ensure the user you are authenticating with has sufficient permissions to create tags in the specified project.
  • Double-check that the artifact reference (e.g., digest or tag) exists in the specified repository.
  • If you continue to face issues, you can use the -v option in curl to see detailed logs of the request and response, which might provide more insights into the problem.