How to create ticket with attachment?

Hi. I want to import some tickets along with attachments from another freshservice tenant.
I have an export of the attachments containing the following info:

ticket_id	54
id	21000006173
created_at	2022-06-21T09:14:03Z
updated_at	2022-06-21T09:14:07Z
name	UOS_CHN-68.pdf
attachment_url	https://devtesting2.attachments.freshservice.com/data/helpdesk/attachments/production/21000006173/original/UOS_CHN-68.pdf?response-content-type=application/pdf&Expires=1655890141&Signature=hbssI1usxE5P6-fymYXvSLrU6VAtMKMhnBiVFuroxzqarslf-bwEwgiUf8schFBpt~y-0Dj-ofeVjf3KX9XsWQ1xqjCu1KAjnpggnQx396a1ZnJp2yLLnOn5OKt3Kwo3-CHl-2Zruav0lvSW1T7bi8vxo8O1k0FdudbfAc~CtqoiWmYcdu~1C~rsN0BP5ltpLSROdcWv8w7bmOXkoySuszbRvhqde-LppOWTPUDePHRPamRk5US9AbEWYDG28rINi23QrqWMKsCUL0NwjeP6kylG35Gse4-HIT-qwYIPQmm~X6k7DbmftuE8oKDf2mOzRXGZSlWeifHJ4Qq6yl4B4w__&Key-Pair-Id=APKAIPHBXWY2KT5RCMPQ
content_type	application/pdf
size	178568
canonical_url	https://devtesting2.freshservice.com/helpdesk/attachments/21000006173

The create ticket documentation (Service Desk API for Developers | Freshservice) just says “array of attachments” as the attachment input. Can you clarify how I could import the above example alongside the ticket? (a curl example would be amazing)

Many thanks,
Michael D

Hi @dawsonweb

Following is from the Create Ticket API documentation

curl -v -u user@yourcompany.com:test -F 'attachments[]=@/Users/user/Desktop/api_attach.png' -F 'subject=Support Needed...' -F 'description=Details about the issue...' -F 'email=tom@outerspace.com' -F 'priority=1' -F 'status=2' -X POST 'https://domain.freshservice.com/api/v2/tickets'

In order to create ticket with attachment using the API, you need to download the attachments locally and then use the create ticket API as shown in example.

1 Like

Thanks @Sachin_Kumar can you tell me is each element of the attachments array a base64 encoded string?

@dawsonweb,

Each element of the attachment array is the local file path.

Apologies i’m not being very clear. I’m actually using PowerShell to invoke the Post request to create a ticket with attachment. See below…

    foreach ($ticket in $tickets) {

        $body = @{
            "description" = $ticket.description
            "subject"     = $ticket.subject
            "email"       = $ticket.email
            "priority"    = $ticket.priority
            "status"      = $ticket.status
            "attachments" = $data
        } | ConvertTo-Json
    
        $header = @{
            "Content-Type"  = "application/json"
            "Authorization" = "Basic azcyRk1tMk8zSUIyaDFMakE4bE06WA=="
        } 
        $body
        Invoke-RestMethod -Uri "https://devtesting2.freshservice.com/api/v2/tickets" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML

    }

Invoke-Restmethod supports an -Infile parameter, I just can’t understand how I need to encode the attachment in the same way curl does to Post it.

Thanks so much for your help.

Hi @dawsonweb

I am not so familiar with PowerShell however to create a ticket with attachment we need to set the header “content-type” as “multipart/form-data”.
I looked on internet and looks like PowerShell does not have the ability to do so.
Found one topic in stackoverflow on sending files using PowerShell and REST API.

Hope this helps.

1 Like