1

I was trying to automate creating ENV variables in CI/CD by using API commands, but unfortunately getting 401 Unauthorized error.

Earlier I used to do this in same way and was able to do it.
But now it’s throwing an error, could anyone please help me to find out.

Command:

curl --request POST --header “PRIVATE-TOKEN: <your_access_token>” \
  “https://gitlab.com/api/v4/projects/1/variables” --form “key=NEW_VARIABLE” --form “value=new value”

Please look into the error message:

error

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250

1 Answers1

0

The "Create a variable" API call indeed looks like:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
 "https://gitlab.com/api/v4/projects/1/variables" --form "key=NEW_VARIABLE" --form "value=new value"

(make sure to use the right double-quotes " instead of )

Double-check the ID of the project (in your case "1") and make sure the user authenticated with your token has the right permissions:

They must be "maintainer" or "owner", in order to have the right to "Manage project-level CI/CD variables".


Example, using a Personal Access Token with api scope, starting with glpat- (glpat-xxxxxxx), I can first list my projects (using jq):

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/owned?=true"|jq ".[] | \"\(.id) \(.path_with_namespace)\""

That allows me to find the project id

I can then list variables for an existing project:

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"

Result:

[]

I have none on that project.
I will set one with:

curl -XPOST --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables" --form "key=NEW_VARIABLE" --form "value=new value"

Result:

{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}

Let's double-check with:

curl -XGET --header "PRIVATE-TOKEN: glpat-xxxx" "https://gitlab.com/api/v4/projects/<projectId>/variables"

Result:

[{"variable_type":"env_var","key":"NEW_VARIABLE","value":"new value","protected":false,"masked":false,"environment_scope":"*"}]

It does work.


The OP Anirban Das confirms in the comments an issue with how Postman was used:

Actually in the body of Postman, select 'form', there I had mentioned directly key name in the Key section and value in the Value section.
But that was not correct.

In the Key section, we need to mention "key" and key name should be in Value section.
Similarly "value" in key section and it's value in Value section.

Once this worked, you will see "</>" icon in right navigation pane, which will provide you corresponding curl command

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, I have given correct project ID and used double quote as well (""). PFA screenshot. Also, in this project I am owner. – Anirban Das Sep 30 '22 at 07:13
  • I tried to call API from Postman, there i am getting "error: key is missing, value is missing" error. – Anirban Das Sep 30 '22 at 07:15
  • @AnirbanDas That soulds like your potman query did not send the form values. – VonC Sep 30 '22 at 08:34
  • yes. can you pls try from end and show me how it's behaving @VonC – Anirban Das Sep 30 '22 at 14:25
  • @AnirbanDas I just tested the command. It does work. I have edited the answer to include an example. – VonC Sep 30 '22 at 15:38
  • still not working for me, looks like something missing still. Can you pls let me know whether there is any pre-requisites to execute API commands apart from personal access token. @VonC – Anirban Das Sep 30 '22 at 17:07
  • @AnirbanDas the PAT (Personal Access Token) is the main prerequisite, provided it authenticates you, and with the right scope (api). Is your PAT starting with `glpat-`? – VonC Sep 30 '22 at 18:07
  • yes. even I worked on POST API earlier, then worked fine, but not sure why not working now. GET is working from Postman, but not with cURL – Anirban Das Oct 01 '22 at 01:19
  • @AnirbanDas Do you have a `~/.curlrc` file? A proxy? What is you OS and OS version, shell and shell version? – VonC Oct 01 '22 at 07:05
  • finally able to do it from Postman and then copied corresponding curl command, that worked perfectly. Thanks much for your time :) – Anirban Das Oct 01 '22 at 17:17
  • @AnirbanDas Great! What did you change/fix on postman in order to make that `curl` command work? – VonC Oct 01 '22 at 19:39
  • Actually in the body of Postman, select 'form', there I had mentioned directly key name in the Key section and value in the Value section. But that was not correct. In the Key section, we need to mention "key" and key name should be in value section. Similarly "value" in key section and it's value in value section. – Anirban Das Oct 02 '22 at 01:38
  • once this worked, you will see ">" icon in right navigation pane, which will provide you corresponding curl command. – Anirban Das Oct 02 '22 at 01:39
  • @AnirbanDas Thank you so much for this feedback, I am sure it will help others. I have included your comment in the answer for more visibility. – VonC Oct 02 '22 at 20:01