1. Issue Summary: There is no proper document to create an attachment to existing freshservice ticket. I am using Typescript to attach CSV file to a ticket. Can you give me a solution what information Freshservice API needs? Its always complaining there is invalid field and no information about to make it valid.
2. typescript code to test. All you have to do is create a typescript project and do needed modules and you can test it out.
import fs, { createWriteStream } from "fs";
import FormData from "form-data";
const createFreshworkAttachment = async () => {
console.log("Processing FRESHWORKS attachment to task.." );
let response = true;
let url = "https://xxxxx.freshservice.com/api/v2/tickets/9535/notes";
const fileName = "/home/praba/wkspace/my-typescript/attachment.csv";
const form = new FormData();
//form.append("attachments[]", new Blob([fs.readFileSync(fileName)]), fileName);
form.append("attachments[]", fs.createReadStream(fileName), 'text/plain');
form.append("private", "true");
form.append("body", "<html><body><pre>Violdation details are in the attachments</pre></body></html>");
const headers: Headers = new Headers();
const authorization = `Basic ${Buffer.from('---------------------:X').toString("base64")}`; // Remove token for security
headers.set("Authorization", authorization);
headers.set("Content-Type", "multipart/form-data");
//headers.set("Content-Type", "application/json");
console.log(JSON.stringify(form));
try {
// const fields = [];
// fields.push("private", true);
// fields.push("body", "Testing body file");
// fields.push("attachments", [fs.createReadStream(fileName)]);
const options: RequestInit = {
method: "POST",
//url: url,
//port: 443,
headers: headers,
// @ts-ignore
body: JSON.stringify(form),
//body: {message:"<html><body><pre>Violdation details are in the attachments</pre></body></html>"},
// formData: {
// "attachments[]":fs.createReadStream(fileName),
// //"body": "Violdation details are in the attachments"
// },
};
await fetch(url, options)
.then((response) => {
console.log(`Response: ${response.status} ${response.statusText}`);
return response.text();
})
.then((text) => console.log( `FRESHWORKS Attachment response ${text}` ))
.catch((err) => console.log( "attachment error"+ err ));
} catch (error) {
console.log(error);
response = false;
}
return response;
};
console.log("before");
createFreshworkAttachment();
console.log("after");
3. Kindly add screenshots or screen grabs that will help us understand your situation better. Feel free to attach images with your post or Dropbox / Google Drive / Box links
4. Logs:
Response: 400 Bad Request
FRESHWORKS Attachment response {“description”:“Validation failed”,“errors”:[{“field”:“{"_overheadLength":363,"_valueLength":82,"_valuesToMeasure":”,“message”:“Unexpected/invalid field in request”,“code”:“invalid_field”}]}