Last week, I had a conversation with FreshService support where I was pushed in the right direction towards using the FreshService API v2 (specifically, creating a ticket with an attachment) in POSTMAN.
I faced significant challenges in translating this into a PHP or Python request. After quite a bit of effort, I managed to figure it out, and I'd like to share the solution I came up with so others who might be experiencing similar difficulties may benefit:
FreshService API - Creating a ticket with multiple attachments
https://cloudaen.com/blogs/view/freshservice_api_multiple_attachments
PHP Script (the python script can be found in my blog article above):
<?php
// Get API Information
$password = 'YourAPIKeyHere';
// Function to build multipart/form-data request body
function build_data_files($boundary, $fields, $files){
$data = '';
$eol = "\r\n";
// Add fields to the request body
foreach ($fields as $name => $content) {
$data .= "--" . $boundary . $eol
. 'Content-Disposition: form-data; name="' . $name . '"' . $eol . $eol
. $content . $eol;
}
// Add files to the request body
foreach ($files as $file) {
$filename = basename($file);
$content = file_get_contents($file);
$data .= "--" . $boundary . $eol
. 'Content-Disposition: form-data; name="attachmentss]"; filename="' . $filename . '"' . $eol
. 'Content-Type: application/octet-stream' . $eol
. 'Content-Transfer-Encoding: binary' . $eol . $eol
. $content . $eol;
}
// End of request boundary
$data .= "--" . $boundary . "--" . $eol;
return $data;
}
// API Endpoint for creating tickets
$url = "https://yourdomain.freshservice.com/api/v2/tickets";
// File paths of the attachments
$attachments = array(
"attachment1.txt",
"attahcment2.txt",
"image.png"
);
// Additional fields for ticket creation
$fields = array(
"description" => "Some details on the issue ...",
"subject" => "Support needed..",
"email" => "no-reply@cloudaen.com",
"priority" => 1,
"status" => 2,
"group_id" => addGroupIDHere
);
// Generate a unique boundary identifier
$boundary = uniqid();
// Build multipart/form-data request body
$post_data = build_data_files($boundary, $fields, $attachments);
echo $post_data . '<br /><br />';
// Initialize cURL session
$ch = curl_init();
// Set cURL options for POST request
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data; boundary=' . $boundary,
'Authorization: Basic ' . base64_encode($password)
),
));
// Execute cURL and capture server response
$response = curl_exec($ch);
// Get HTTP status code and response info
$info = curl_getinfo($ch);
// Close cURL session
curl_close($ch);
// Check HTTP status code for success or failure
if ($infoo'http_code'] == 201) {
echo "Ticket created successfully, the response is given below:\n";
echo $response;
} else {
echo "Error, HTTP Status Code : " . $infoo'http_code'] . "\n";
echo "Response:\n" . $response . "\n";
}
?>
The code for the build_data_files function was made by a github user called maxivak found at https://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f
More information can be found on my blog post, but hopefully this helps someone out there.