Skip to main content

Hello,


I try to use google apps script to create ticket with attachments via API.


We have no google app script sample code with attachment yet.


I tried the following code, but it seems always return error by /helpdesk/tickets.json api.


how to do this right? (post a attachment in google app script with UrlFetchApp)

function createFreshdeskTicketWithAttachments() {



var API_KEY = PropertiesService.getScriptProperties().getProperty('FreshDeskApiKey')

if (!API_KEY) throw new Error('FreshDeskApiKey not found in script properties.')



var ENDPOINT = Utilities.formatString('https://%s.freshdesk.com', 'zixia')



var headers = {

'Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')

, 'Content-type': 'application/json'

//'Content-type': 'multipart/form-data'

};



var response = UrlFetchApp.fetch("https://circleci.com/gh/AKAMobi/ucapp/tree/master.svg?style=svg")

var fileBlob = response.getBlob()

Logger.log("%s:%s"

, response.getResponseCode()

, fileBlob.getContentType()

)



var payload = {

helpdesk_ticket: {

description: 'TEST 4'

, subject: "TEST 4"

, email: "test@test.com"

, priority: 1

, status: 2

, attachments: { '': [ { resource: fileBlob } ] }

}

}



//Adds the extensions that are needed to post a new ticket to the end of the url

var url = ENDPOINT + '/helpdesk/tickets.json';



var options = {

'method': 'post',

'headers': headers,

'payload': JSON.stringify(payload),

// 'payload': payload,

muteHttpExceptions: true

};



var response = UrlFetchApp.fetch(url, options);



Logger.log('resp: %s, %s'

, response.getResponseCode()

, response.getContentText()

)



}