Hey Paul
I tried doing the following on our system and managed to break everything
curl octopus/api/projects/projects-98/variables/49c6c06e-54ea-4bdb-a7da-a4596e9ef3ab -XPUT -d
"{
"Name": "MyVariableName",
"Value": "TheValue"
}"
What I get back is a 200 OK with the Name, Value, Environment and StepId set to null.
{
"Id": "49c6c06e-54ea-4bdb-a7da-a4596e9ef3ab",
"Name": null,
"Value": null,
"ProjectId": "projects-98",
"Role": null,
"EnvironmentId": null,
"StepId": null,
"Links": {
"Self": "/api/projects/projects-98/variables/49c6c06e-54ea-4bdb-a7da-a4596e9ef3ab",
"Project": "/api/projects/projects-98"
},
"IsEditable": true,
"IsVisible": true
}
I try and log into octopus and fix the issue and everything is broken. Upon querying the api it spits out a json parse error.
To fix it I had to go into raven and change the data manually.
Okay so the question is ā¦
How can I set a project variable using the REST API?
Thanks again
Antony
Hi Antony,
First, you would need to do a āPOSTā rather than a āPUTā to add a variable. If you want to update an existing variable, you would have to list them, then work out the one to change, and then do a PUT including the Id
of the variable (the GUID) to update. This is because a variable can be defined multiple times with different scopes.
I think youāll also need to include the Content-type: application/json
header in your request so that our model binder knows you are sending JSON. Currently it seems to be ignoring the content.
Hope that helps,
Paul
Hey Paul thanks it was essentially the content-type causing problems (Iād specified it incorrectly in the client I was using).
And yes I was trying to update an existing variable, I should have been clearer in my question.
Some feedback and opinions on behavior I didnāt expect, no doubt there in your back log but I hope you donāt mind me giving my pennies worth.
If Iām trying to update the value only, when providing the payload
{
"value":"newvalue"
}
In my option no other values should change other than āvalueā rather than setting the entire resource. As a side if you donāt specify name it getās set to null and causes everything to break with parse errors. If its required put some validation on it and let me know if I fail to meet it.
Perhaps itās more of an ideological vs practical argument. Should I have to specify the complete resource when trying to set it? Maybe I should but if I did it would be nice to get some hints about the ones that I do, even if I have to explicitly set them to null, something like this perhapps?
{
"Name": "ExternalDataApi",
"Value": "https://sssdataservices-api.fundapps.co/",
"Role": null,
"EnvironmentId": "DeploymentEnvironments-353",
"StepId": null
}
Then anything else just returns a validation error.
Thanks again
Antony
Hi Anthony,
For 2.0 weāre working on some changes to the API, and this will include more documentation and better validation for activities like this. Youāll get validation errors when required fields arenāt provided, and there will be some metadata available to describe the fields that can be changed from the API.
Currently the API exists alongside the website, so it doesnāt get a lot of love. For 2.0 weāll actually be making the API the primary way of interacting with Octopus, and the web portal will be rewritten so that it works solely through the API.
(That said, Iām not sure if weāll support a patching API like you describe, itās likely that weāll still expect the full resource to be PUT back)
Paul