Create note / ticket with json file attachment

 Hi all,


I try to create a note to an existing ticket with an attachment without success.


I try with the API example without issue :

curl -v -u APIKEY:X -F "attachments[]=@test.jpeg" -F "attachments[]=@test1.jpeg" -F "body=this is a sample ticket" -X POST 'https://DOMAIN.freshdesk.com/api/v2/tickets/40/notes'


But i want to try to add an attachment with Json, like :

curl -v -u APIKEY:X -H "Content-Type: application/json" -d '{"attachments" : {"0":"test.jpeg"}, "body": "this is a sample ticket"}' -X POST 'https://DOMAIN.freshdesk.com/api/v2/tickets/40/notes'

Result :

{"message":"We're sorry, but something went wrong."}


It's possible ?


Thank you in advance.


Regards,

Hugo


2 people have this question

I see this in the API documentation :

The Content-Type should always be multipart/form-data for create/update requests with attachments.


So, I think it's not possible

Hello Hugo,


Did you get a chance to look at our Github repo? This sample written in php might help you to achieve this.


Cheers!

Hello Aravind,


Thanks for your solution.


I am now stuck to create a new ticket with multiple files.

In my parameters :

array (size=9)
  'custom_fields' => 
    array (size=2)
      'cf_order_number' => string '1772684' (length=7)
      'cf_model' => string '2913711' (length=7)
  'subject' => string 'Sinister' (length=8)
  'description' => string '785785' (length=6)
  'email' => string 'test@ŧest.com' (length=24)
  'priority' => int 1
  'status' => int 2
  'attachments[0]' => string '@/filer/data/upload/fo/912616/74fa5e2cbcff35860eea50b1575d4017.jpg' (length=66)
  'attachments[1]' => string '@/filer/data/upload/fo/912616/b9121362d89879fc15c22bae3be077d0.jpeg' (length=67)
  'attachments[2]' => string '@/filer/data/upload/fo/912616/e241ae160115a325bb2fb50cdfc5bdb5.jpg' (length=66)

The return message is :

public 'message' => string 'We're sorry, but something went wrong.' (length=38)

"attachments[]" can only be a String, by I need to send several images.


Do you have a solution ?


Thank you in advance.


Regards,

Hugo

Hello Aravind,


I found the solution but I think it's impossible in Json to send more of 1 file :


$data = "";
$eol = "\r\n";
$mime_boundary = md5(time());

foreach ($params as $key => $value) {
// Attachments
if ($key == "files") {
foreach ($value as $key => $file) {
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="attachments[]"; filename="' . $file['name'] . '"' . $eol;
$data .= "Content-Type: " . mime_content_type($file['path']) . $eol . $eol;
$data .= file_get_contents($file['path']) . $eol;
}
} else {
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="' . $key . '"' . $eol . $eol;
$data .= $value . $eol;
}
}
$data .= "--" . $mime_boundary . "--" . $eol . $eol;

$header[] = "Content-type: multipart/form-data; boundary=" . $mime_boundary;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERPWD, "$apiKey:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Regards,

Hugo


@Hugo.pascal thx for solution but how can I submit custom fields along with multiple attachments?

 I have this error "[field] => custom_fields [message] => Value set is of type String.It should be a/an key/value pair [code] => datatype_mismatch )" here is my $params

$params = array(
  'email' => $email,
  'subject' => $subject,
  'description' => $description,
  'type' => $type,
  'priority' => $priority,
  'status' => 2,
  'files' => $uploaded_files,
  'custom_fields' => array("cf_product" => $product)
);

 


I am trying to create a ticket with attachments from the browser , I get the same error ===> 

[field] => custom_fields [message] => Value set is of type String.It should be a/an key/value pair [code] => datatype_mismatch )

Login or Signup to post a comment