RT REST 2 API error parsing json contening attached file

I updated my code to create a ticket using RT REST 2 API to attach an uploaded file and i got this issue:

{“message”:“JSON parse error: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before “(end of string)”)\n”}

Here is my code:

$headers = array(
“Content-type: application/json”,
“Authorization: token 1-14-c6cde7f274dcbf4f8d1bf2da32449518”
);
$full_path=DIR.’/design-02.png’;
$curlFile = curl_file_create($full_path,mime_content_type($full_path),‘design.png’);

$ticket_data=array(
“Queue” => “General”,
“Subject” => “hello world”,
“Content” => “That damned printer is out of order again!”,
“ContentType”=> “text/html”,
“CustomFields”=>array(‘Phone’=>‘65666’,‘UploadField’=>array(‘fichier1’=>array(‘FileContent’=> file_get_contents($curlFile->getFilename()),‘FileName’=>$curlFile->getPostFilename(),‘FileType’=>$curlFile->getMimeType())))
);
$post = $ticket_data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$base_url.‘ticket’);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
$resp= json_decode($result,true);
curl_close ($ch);

Thanks for the support

This has been my first time trying PHP, its been interesting. For uploading an attachment I am not sure you can add one on create, but I know it can be done on comment/reply:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'MyRT/REST/2.0/ticket/41/comment',
  CURLOPT_RETURNTRANSFER   => true,
  CURLOPT_ENCODING         => '',
  CURLOPT_MAXREDIRS        => 10,
  CURLOPT_TIMEOUT          => 0,
  CURLOPT_FOLLOWLOCATION   => true,
  CURLOPT_HTTP_VERSION     => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST    => 'POST',
  CURLOPT_POSTFIELDS       => '{
    "Queue"        : "General",
    "Subject"      : "Some subject",
    "Content"      : "Have you seen this <b>image</b>",
    "ContentType"  : "application/json",
    "Subject"      : "HTML comment with PNG image and text file",
    "Attachments"  : [
            {
                "FileName" : "some image.png",
                "FileType" : "image/png",
                "FileContent" : "@/somepath/test.png"
            }
        ]
  }',
  CURLOPT_HTTPHEADER => array(
    'Authorization : Basic cm9vdDpwYXNzd29yZA==',
    'Content-Type  : application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

This was generated from Postman ^

Ok thanks, i try it now

You are correct, you can’t add attachments on create with the standard REST2 API. You need the patches here: Allow adding attachments on ticket create by puck · Pull Request #42 · bestpractical/rt-extension-rest2 · GitHub

Cheers,
Andrew

Ok Thank you Andrew. i did already using customfields, so i created a CusFomfield on RT and provide all the required information on ticket creation (FileName, FileContent and FileType). Here is my code:

function rtc_report_incident($post_data,$post_files=array()){
$base_url= rtc_get_base_url();
$headers = array(
“Content-type: application/json”,
“Authorization: token 1-14-c6cde7f274dcbf4f8d1bf2da32449518”,
);
// $headers= rtc_get_header();
$files=array();
//uploaded files
if(!empty($post_files)){
// Count # of uploaded files in array
total = count(_FILES[‘upload’][‘name’]);
// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {

   //Get the temp file path
   $tmpFilePath = $post_files['upload']['tmp_name'][$i];
   $filename= basename($post_files['upload']['name'][$i]);

//Make sure we have a file path
if ($tmpFilePath != “”){
//Setup our new curl file
$curlFile = curl_file_create($tmpFilePath,mime_content_type($tmpFilePath),$filename);
//Upload the file into the temp dir
// if(move_uploaded_file($tmpFilePath, $newFilePath)) {
// }
$single_file=array(‘FileContent’=> base64_encode(file_get_contents($curlFile->getFilename())) ,‘FileName’=>$curlFile->getPostFilename(),‘FileType’=>$curlFile->getMimeType());
$files[]=$single_file;
}
}
}

//$full_path=DIR.’/design-02.png’;

$ticket_data=array(
“Queue” => “General”,
“Subject” => $post_data[‘subject’],
“Requestor” => $post_data[‘email’],
“Content” => $post_data[‘message’],
“ContentType”=> “text/html”,
“CustomFields”=>array(‘Phone’=>$post_data[‘phone’],‘UploadFields’=>$files)
);
$post = $ticket_data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$base_url.‘ticket’);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result=curl_exec ($ch);
$resp= json_decode($result,true);
//var_dump($result);
curl_close ($ch);

return $resp;

}

Thanks to all for the suport

Excellent!

And just to let you know, the code for adding attachments on ticket create via the REST2 API has been included in RT 5.0.1.

Cheers,
Andrew