fresh-samples icon indicating copy to clipboard operation
fresh-samples copied to clipboard

PHP: validation error for all ticket fields when setting header content-type "multipart/form-data"

Open marcospada opened this issue 6 years ago • 2 comments

[using php 5.6 on a windows machine for testing] Hello, I've followed this guide to upload an attachment https://github.com/freshdesk/fresh-samples/blob/master/PHP/create_ticket_with_attachment.php and it worked when launched directly without using an html form as data source and without setting any header via curl_setopt() (it's also working without attachments using the default content-type 'application/json', but this is not what I need ) the request header, using the basic example (no html post request but just sample data created on the fly) looks like this (I assume the content-type and boundary are set somewhere under the curtain)

Host: xxxx.freshdesk.com Authorization: Basic xxxxxxxxxxxxxxxxx== Accept: */* Content-Length: 79971 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------d7fb41f0a8e1064b

now, my problem is when I try to use this code on an actual POST from a user, the request's content-type is automatically set to

application/x-www-form-urlencoded

even though I specified in the html form the enctype "multipart/form-data" so I had to curl_setopt the content-type header

$headers[] = "Content-type: multipart/form-data;"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

My API call now looks like this

    $ticket_payload = array(
       'name' => $requester_name,
       'email' => $requester_email,
       'subject' => 'XXX' - new ticket: '.$ticket_data['subject'],
       'description' => $ticket_data['description'],
       'priority' => intval($ticket_data['priority']),
       'status' => intval($ticket_data['status']),
       'source' => intval($ticket_data['source']),
       'type' => $type,
       'responder_id' => $this->default_agent_id,
       'attachments[]' =>  curl_file_create($local_file_path, mime_content_type($local_file_path), $filename),
       'tags' => $ticket_data['tags'],
   );

curl_setopt($this->ch, CURLOPT_POSTFIELDS, $ticket_payload );

which gets an

Error, HTTP Status Code : 400

and in the response, a list of validation errors that includes basically every ticket field it seems that if the attachment is there, and the mutipart/form-data header with it, the actual ticket payload (i.e. user-POSTed data) is lost somewhere along the call

How can I post both data and a file ? I took a look also at this other example https://github.com/freshdesk/fresh-samples/blob/master/PHP/create_ticket_with_multiple_attachments.php which manually writes headers and body of the request for every field and file using a custom 'boundary' but honestly it looks a bit weird to me, no curl_create_file and a <file_get_contents()> call for every file to attach

Looking at issues posted here https://github.com/freshdesk/fresh-samples/issues/37 I noticed another user has the same problem with Node.js, and another user here https://github.com/freshdesk/fresh-samples/issues/44 suggests to separate the ticket data from the files data

Can you help me with this please ? User uploaded attachments for incidents and bugs are quite important for us

Thanks in advance Marco

marcospada avatar Apr 30 '18 01:04 marcospada

Hi Marco, I face the same problem. And i solve it for you reference. Let custom_fields index includes the input's name like below.

Hope it helps for you.

$ticket_payload = array( "type" => 'General Inquiry', 'email' => '[email protected]', 'subject' => 'test', 'description' => 'testing description content', 'priority' => 2, 'status' => 2, 'custom_fields[region]' => 'Asia', 'custom_fields[country]' => 'Taiwan', 'custom_fields[cf_model]' => '12345', 'custom_fields[cf_checkbox]' => "true", 'attachments[]' => curl_file_create("images/upload/ttt.png", "image/png", "ttt.png"), );

spencerchang avatar May 17 '18 06:05 spencerchang

Hi Spencer, thanks for your tips, but your code doesn't work for me, my problem is not with custom fields, but with the tickets main fields (which cannot be put inside some custom ones), when the attachments[] array is present I solved it, finally, on my own: I found out (http://php.net/manual/en/function.curl-setopt.php#107146) that when using CURLOPT_POST with CURLOPT_POSTFIELDS as an array that the content length is cut (I don't understand why) So avoiding to set the CURLOPT_POST option solved my problem

marcospada avatar May 23 '18 17:05 marcospada