Error while creating asset with Contentful Management SDK - contentful

I'm using the Content Management JS SDK, version 5.21.1
Trying to create a new image asset but getting an error. I've attempted uploading the image first and calling createAsset:
const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const upload = await environment.createUpload({ file: bytes });
const asset = await environment.createAsset({
fields: {
title: {
[this.locale]: title
},
description: {
[this.locale]: description
},
file: {
[this.locale]: {
fileName: fileName,
contentType: contentType,
uploadFrom: {
sys: {
type: 'Link',
linkType: 'Upload',
id: upload.sys.id
}
}
}
}
}
});
asset.processForAllLocales();
return await asset.publish();
I've also tried using createAssetFromFiles directly:
const space = await this.client.getSpace(process.env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment(this.environment);
const asset = await environment.createAssetFromFiles({
fields: {
title: {
[this.locale]: title
},
description: {
[this.locale]: description
},
file: {
[this.locale]: {
fileName: fileName,
contentType: contentType,
file: bytes
}
}
}
});
asset.processForAllLocales();
return await asset.publish();
Here's the error I get (same for both calls):
{
"status": 422,
"statusText": "Unprocessable Entity",
"message": "Validation error",
"details": {
"errors": [
{
"name": "required",
"path": [
"fields",
"file",
"en-US",
"url"
],
"details": "The property \"url\" is required here"
}
]
},
"request": {
"url": "assets/01Ft5vBdTHzJPzIVJdBOlE/published",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/vnd.contentful.management.v1+json",
"X-Contentful-User-Agent": "sdk contentful-management.js/5.21.1; platform node.js/v13.12.0; os macOS/19.4.0;",
"Authorization": "Bearer ...",
"user-agent": "node.js/v13.12.0",
"Accept-Encoding": "gzip",
"X-Contentful-Version": 1
},
"method": "put",
"payloadData": null
},
"requestId": "07778ff81872ddb45d5e1a7266436e22"
}
From reading your docs, I was under the impression the URL gets automatically created after the asset is created so I'm not sure what this error means.
Any help is greatly appreciated!

So found the root cause:
asset.processForAllLocales();
return await asset.publish();
I needed to call publish on the processed asset:
const processedAsset = asset.processForAllLocales();
return await processedAsset.publish();
Unfortunately, SDK error messages are cryptic and documentation is pretty patchy.

Related

How to add parameters to Axios

I am trying to implement axios POST to itop, everything works fine using POSTMAN for me to hard code "title, description, name and first name". Basically at the first start I want to make it like a webpage to input data and then submit the data to itop using POST.
Instead of me manually adding the required parameters, I want to grab the parameters from a form. As for the current script that I have, it said the URL is wrong because the actual URL should be like this instead of below (actual URL from postman: http://test.com/itop/webservices/rest.php?version=1.3&json_data={\n "operation": "core/create",\n "comment": "Synchronization from blah...",\n "class": "UserRequest",\n "output_fields": "id, friendlyname",\n "fields":\n {\n "org_id": "SELECT Organization WHERE name = \"TEST\"",\n "caller_id":\n {\n "name": "name",\n "first_name": "first_name"\n },\n "title": "Test",\n "description": "Test Ticket"\n }\n}')
Here is the code, this code will give out 404 because the request is not correct
app.post('/form-submit', (req, res) => {
let bodyData = {
"operation": "core/create",
"class": "UserRequest",
"output_fields": "id, friendlyname",
"fields": {
"org_id": "SELECT Organization WHERE name = 'TEST'",
},
"caller_id": {
"name": req.body.name,
"first_name": req.body.first_name
},
"title": req.body.title,
"description": req.body.description,
}
let axiosConfig = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic c2FtdWVsLmNoYW5AbHlasdfasdZS5jb206W04wemgxa2FyMV0=',
'Cookie': 'itop-ffb5bc6b0d3aasdfa53a36b=g20efh3399am02aq7v8tif1b7l'
},
}
axios.post('http://test.com/itop/webservice/rest.php?version=1.3&json_data=', bodyData, axiosConfig)
.then(function (response) {
console.log("Ticket created:" + response.data)
})
.catch(function (error) {
console.log(error)
})
})
Thanks ahead for any insight provided

Can't get a succesful jira create issue api response

I am trying to call jira rest api POST /rest/api/3/issue from a node js application. I am getting the following error even though am passing correct details. I am passing issueType as 10103 and remaining sensitive params as required. Here is my
nodejs code.
app.post("/webhook", function(req,res,next){
console.log(req.body);
let options = {
method: 'POST',
url: req.body.tags["jira:endpointURL"]+"/rest/api/3/issue",
auth: {
user: req.body.tags["jira:user"],
password: req.body.tags["jira:token"]
},
headers: {
'Content-Type': 'application/json'
},
json: {
"update": {},
"fields": {
//"summary": req.body["subject"],
"summary": "Test",
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "body",
"type": "text"
}
]
}
]
},
"issuetype": {
"id": req.body.tags["jira:issueType"]
},
"project": {
"key": req.body.tags["jira:project"]
}
}
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(
'Response: ' + response.statusCode + ' ' + response.statusMessage
);
console.log(body);
res.send("OK");
});
});
Error thrown
summary: "Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.",
description: "Field 'description' cannot be set. It is not on the appropriate screen
You have to configure the fields to the screens first.
See:
https://support.atlassian.com/jira-cloud-administration/docs/add-a-custom-field-to-a-screen/
https://support.atlassian.com/jira-cloud-administration/docs/configure-issue-screens/#Configure-a-screen--x27-s-tabs-and-fields

Open modal using Slack command

I have a Slack command which displays a button. When I click on this button I need to display a modal. For this, after clicking it I do this:
const dialog = {
callback_id: "submit-ticket",
elements: [
{
hint: "30 second summary of the problem",
label: "Title",
name: "title",
type: "text",
value: "teste"
},
{
label: "Description",
name: "description",
optional: true,
type: "textarea"
},
{
label: "Urgency",
name: "urgency",
options: [
{ label: "Low", value: "Low" },
{ label: "Medium", value: "Medium" },
{ label: "High", value: "High" }
],
type: "select"
}
],
submit_label: "Submit",
title: "Submit a helpdesk ticket"
};
const modalInfo = {
dialog: JSON.stringify(dialog),
token, // this is correct
trigger_id: actionJSONPayload.trigger_id
};
// This is what I get confused with...
// Method 1
slack.dialog.open(modalInfo).catch(err => {
console.log("ERROR: ", err);
});
// end method 1
// Method 2
sendMessageToSlackResponseURL(actionJSONPayload.response_url, modalInfo);
...
function sendMessageToSlackResponseURL(responseURL: any, JSONmessage: any) {
const postOptions = {
headers: {
"Content-type": "application/json"
},
json: JSONmessage,
method: "POST",
uri: responseURL
};
request(postOptions, (error: any, response: any, body: any) => {
if (error) {
console.log("----Error: ", error);
}
});
}
// end method 2
I get always Error: invalid_trigger using method1 when this trigger is something that my button is generating automatically.
Method 2 doesn't throw any error but doesn't open any modal/dialog either.
The official documentation is not quite clear and I don't know if I need to call dialog.open or views.open. Either way, the last one is not available from Slack package
This is also the button I'm displaying before anything:
const message = {
attachments: [
{
actions: [
{
name: "send_sms",
style: "danger",
text: "Yes",
type: "button",
value: "yes"
},
{
name: "no",
text: "No",
type: "button",
value: "no"
}
],
attachment_type: "default",
callback_id: "alert",
color: "#3AA3E3",
fallback: "We could not load the options. Try later",
text: "Do you want to alert by SMS about P1 error/fix?"
}
],
text: "P1 SMSs"
};
Copy a modal from here
const headers = {
headers: {
"Content-type": "application/json; charset=utf-8",
"Authorization": "Bearer " + token
}
};
const modalInfo = {
"token": token,
"trigger_id": reqBody.trigger_id,
"view": slack.modal
};
axios
.post("https://slack.com/api/views.open", modalInfo, headers)
.then(response => {
const data = response.data;
if (!data.ok) {
return data.error;
}
})
.catch(error => {
console.log("-Error: ", error);
});
But most importantly, when we do some changes to our app we need to reinstall it and also when we do it, the token changes and this is something I couldn't find on the documentation

Using Request to post from NodeJS

I am running into an issue with posting a json doc via request from my nodeJs app.
The problem is for me how to pass the json doc to post. Here is what works for me
const options = {
url: 'http://localhost/message/send/981f1507-2df2-4011-8f55-5460897fcde6',
method: 'POST',
json: true,
body: { "message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "html",
"content": "</p>\r\n\r\n<ul type=\"disc\">\r\n\t<li><strong>Ready to Use Experiences</strong> – Get instant access to our fully configured and integrated environments allowing you to use and experience the products as a customer would in their own deployment. We’ve set up the products alongside partner solutions so you can experience the entire ecosystem including Office 365, Salesforce, and more. </li>\r\n</ul>"
},
"toRecipients": [
{
"emailAddress": {
"address": "test#ueser.com"
}} ]}}}
request(options, function(err, res, body) {
console.log(res.statusCode)
})
the problem i have is if i want to pass the body from a string like this
var mybody = `{ "message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "html",
"content": "This is a test"
},
"toRecipients": [
{
"emailAddress": {
"address": "test#user.com"
}
}]}}`
and then call it like this
const options = {
url: 'http://localhost/message/send/981f1507-2df2-4011-8f55-5460897fcde6',
method: 'POST',
json: true,
body: mybody
}
request(options, function(err, res, body) {
console.log(res.statusCode)
})
which throws errors like
SyntaxError: Unexpected token " in JSON at position 0
Have you tried using stringify? Node.js has a a built in module called querystring.
const querystring = require("querystring");
const myStringBody = querystring.stringify(myBody);
Then write it to your request.
req.write(myStringBody);

FCM iOS: Push notifications throw invalid argument

I am attempting to implement pushNotifications using Cloud Functions and FCM for iOS but I am consistently thrown this error:
2018-05-21T13:04:00.087Z I sendPushNotifications: Error sending
message: { Error: Request contains an invalid argument.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:149:50
at process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { code:
'messaging/invalid-argument',
message: 'Request contains an invalid argument.' }, codePrefix: 'messaging' }
My implementation in cloud functions are as follows:
exports.sendPushNotifications = functions.database.ref('/conversations/{userUid}/').onWrite((snap, context) => {
const userUid = context.params.userUid
console.log("Triggered user ", userUid)
return admin.database().ref('/fcmToken/' + userUid).once('value', snapshot => {
const values = snapshot.val()
const fcmToken = values.fcmToken
var message = {
"token": fcmToken,
"notification": {
"body": "New message"
},
"apns": {
"headers": {
"apns-priority": "5"
},
"payload": {
"aps": {
"alert": {
"body": "New message"
},
"badge": "1",
"sound": "default"
}
}
}
};
return admin.messaging().send(message)
.then((response) => {
return console.log('Successfully sent message:', response);
})
.catch((error) => {
return console.log('Error sending message:', error);
});
})
})
The frustrating thing is that when I remove the whole "apns" node the code actually works, ie I can receive the push notifications. I suppose this means my setup are all done properly. Once I included the "apns", it starts throwing the above error. I also reference these three posts, this, this and this, and made sure that I have carefully followed the code and instructions. For some reasons I cannot get it to work.
I also attempted to remove the "notification" node as the docs did mention that only use common keys when targetting all platforms. Since I am targetting only iOS for now, I suppose I should remove the "notification" key. But again it also throws the same error.
Ok, so it was a rookie mistake. It is correct that common keys should not be used if I am targetting iOS only. In addition to that, the badge should be an Int and not String.
This code worked:
var message = {
"token": fcmToken,
"apns": {
"headers": {
"apns-priority": "5"
},
"payload": {
"aps": {
"alert": {
"body": "New message"
},
"badge": 1,
"sound": "default"
}
}
}
}
Hope it helps anyone out there facing the same problem.
Just to add to this answer. If you are using both IOS and android while having custom sounds for both, the code below will work cross platform and avoid this issue.
const payload = {
token,
notification: {
title: `title text`,
body: `body text`,
},
android: {
// android
priority: "high", // legacy HTTP protocol (this can also be set to 10)
notification: {
channel_id: "call1",
priority: "high", // HTTP v1 protocol
notification_priority: "PRIORITY_MAX",
sound: "sound",
default_sound: true,
visibility: "PUBLIC",
},
},
apns: {
// apple
headers: { "apns-priority": "10" },
payload: {
aps: {
// If present with notification: {...}, this will create errors
// "alert": {
// "title": `title text`,
// "body": `body text`,
// },
badge: 1,
sound: {
critical: 1,
name: "sound.aiff",
volume: 1,
},
category: "call1",
},
},
},

Resources