I’m trying to save a project property configuration
. This property doesn’t exist yet so I’m assuming I make a PUT request to set it for the first time. I’m referencing the API docs https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-properties/#api-rest-api-3-project-projectidorkey-properties-propertykey-put.
However my connect app is logging an error:
Error saving project property:"{\"errors\":{},\"errorMessages\":[\"The property value can not be empty.\"],\"httpStatusCode\":{\"empty\":false,\"present\":true}}"
The request from the front-end to the back-end seems to be OK and the call is being made to the REST API, I’m just not sure what the issue could be – is there some specific way that I need to format the data property in the PUT request options? Or do I need to follow some other method to create the project property first?
Thanks!
routes/index.js
...
const doPUT = (httpClient, options) => {
return new Promise((resolve, reject) => {
httpClient.put({
url: options.url,
data: options.data,
headers: {
'Content-Type': 'application/json',
},
}, (error, response, body) => {
if (error || response.statusCode < 200 || response.statusCode >= 300) reject (error || body);
resolve({statusCode: response.statusCode, message: body});
})
})
}
const saveProjectProperty = async ({ configuration, httpClient, projectId }) => {
const url = `/rest/api/3/project/${projectId}/properties/configuration`;
try {
const response = await doPUT(httpClient, { url, data: configuration });
console.log('PUT request result:', response);
return('Success');
}
catch (error) {
console.log(error);
console.log("Error saving project property:" + JSON.stringify(error));
}
};
const saveConfiguration = async (req, res) => {
const httpClient = addon.httpClient(req);
try {
await saveProjectProperty({ configuration: req.body.configuration, httpClient, projectId: req.body.projectId });
res.json({ message: 'Configuration saved!' });
} catch (error) {
console.error('Error processing data:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
app.post('/api/saveConfig', [
addon.authenticate(true),
addon.authorizeJira({ project: ["ADMINISTER_PROJECTS"] })
], safeHandler(saveConfiguration));
...
my-page.hbs
...
<script>
...
export const saveConfiguration = ({ AP, configuration, projectId, token }) => {
fetch('/api/saveConfig', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `JWT ${token}`
},
body: JSON.stringify({
projectId,
configuration
}),
})
.then(response => response.json())
.then(data => {
AP.flag.create({ title: 'Saved', body: data.message, type: 'succes' });
})
.catch(error => console.error('Error fetching data:', error));
}
let token = '{{token}}';
AP.context.getToken((t) => {
token = t;
});
const configuration = {
key: 1,
};
configurationForm.addEventListener('submit', (e) => {
e.preventDefault();
saveConfiguration({ AP, configuration, projectId: '{{projectId}}', token });
});
...
</script>
...