(Solved) Encoding error on attached file (BASE64 NOT DECODED) - 02473109 - RESOLVED

Forum for users and developers of Bullhorn's new REST API service.

Moderators: StaffingSupport, s.emmons, BullhornSupport

mavieo
User
Posts: 11
Joined: Mon Apr 10, 2017 2:01 pm

(Solved) Encoding error on attached file (BASE64 NOT DECODED) - 02473109 - RESOLVED

Postby mavieo » Thu Apr 13, 2017 2:30 pm

The following results in an invalid file in BH portal. When uploading a text document, the contents are very clearly base64 encoded. Please assist if you've figured out this issue.

Code: Select all

$multipart_name = !isset($file['name']) || empty($file['name']) ? 'Unknown' : $file['name'];
            $multipart_filename = !isset($file['filename']) || empty($file['filename']) ? 'unknown' : $file['filename'];

            $multipart_new_line = "\r\n";
            $multipart_boundary = md5(time());
            $multipart_body  = '--'.$multipart_boundary.$multipart_new_line;
            $multipart_body .= 'Content-disposition: form-data; name="'.$multipart_name.'"; filename="'.$multipart_filename.'"'.$multipart_new_line;
            $multipart_body .= 'Content-type: application/octet-stream'.$multipart_new_line;
            $multipart_body .= 'Content-Transfer-Encoding: base64'.$multipart_new_line.$multipart_new_line;
            $multipart_body .= chunk_split(base64_encode(file_get_contents($file['path']))).$multipart_new_line;
            $multipart_body .= '--'.$multipart_boundary.'--'.$multipart_new_line.$multipart_new_line;

            $endpoint_url_qs = empty($endpoint_args) ? '' : http_build_query($endpoint_args);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $endpoint_url.$endpoint_url_qs);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::ENDPOINT_TIMEOUT);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); # Don't use CURLOPT_PUT (results in a "read timed out" error [400])
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart_body);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
               'Content-Type: multipart/form-data; boundary='.$multipart_boundary,
            ));
Last edited by mavieo on Fri Apr 14, 2017 11:08 am, edited 3 times in total.

gwillmottBH
User
Posts: 75
Joined: Tue Apr 12, 2016 10:34 am

Re: Encoding error on attached file (BASE64 NOT DECODED)

Postby gwillmottBH » Thu Apr 13, 2017 2:49 pm

Hello Mavieo,

Can you provide the REST call including the raw json and I will take a look.

Thank you,
Greg
Greg Willmott
Senior Enterprise Support Analyst
B U L L H O R N
Staffing and Recruiting Software, On Target, On Demand
100 Summer St., 17th Floor, Boston, MA 02210
617-478-9126 (US Support)
+44 800 032 2848 ext. 9131 (UK Support)

mavieo
User
Posts: 11
Joined: Mon Apr 10, 2017 2:01 pm

Re: Encoding error on attached file (BASE64 NOT DECODED) - 02473109

Postby mavieo » Fri Apr 14, 2017 10:06 am

This is when uploading a "resume.txt" file.

The file contents: "Test resume"
BH file contents: "VGVzdCByZXN1bWU="

https://rest34.bullhornstaffing.com/res ... xt%2Fplain

The raw JSON response? It just returns the file identifier as if everything worked correctly (because the file does in fact get uploaded + attached to candidate). { fileId: 12345 }

This is what's being set as POSTFIELDS

Code: Select all

--73cb0bb1fd7e2281e7d171ee41cab2fd
Content-disposition: form-data; name="Resume"; filename="tst-guy-resume.txt"
Content-type: application/octet-stream
Content-Transfer-Encoding: base64

VGVzdCByZXN1bWU=

--73cb0bb1fd7e2281e7d171ee41cab2fd--



mavieo
User
Posts: 11
Joined: Mon Apr 10, 2017 2:01 pm

Re: Encoding error on attached file (BASE64 NOT DECODED) - 02473109

Postby mavieo » Fri Apr 14, 2017 11:08 am

Solved. BH doesnt' know what to do with base64 data, changed to binary. Works.

Code: Select all

# ...

if(!isset($endpoint_args['file'])) :
   $this->errorsSet('missing argument "file"', __LINE__, __METHOD__);
   return FALSE;
endif;

$file = $endpoint_args['file'];
unset($endpoint_args['file']);

if(FALSE === is_readable($file['path'])) :
   $this->errorsSet('Failed to locate path', __LINE__, __METHOD__);
   return FALSE;
else :
   $file['contents'] = file_get_contents($file['path']);
endif;

$multipart_name = !isset($file['name']) || empty($file['name']) ? 'Unknown' : $file['name'];
$multipart_filename = !isset($file['filename']) || empty($file['filename']) ? 'unknown' : $file['filename'];

$multipart_new_line = "\r\n";
$multipart_boundary = md5(time());
$multipart_body  = '--'.$multipart_boundary.$multipart_new_line;
$multipart_body .= 'Content-Disposition: form-data; name="'.$multipart_name.'"; filename="'.$multipart_filename.'"'.$multipart_new_line;
$multipart_body .= 'Content-Length: '.strlen($file['contents']).$multipart_new_line;
$multipart_body .= 'Content-Type: application/octet-stream'.$multipart_new_line;
$multipart_body .= 'Content-Transfer-Encoding: binary'.$multipart_new_line.$multipart_new_line;
$multipart_body .= $file['contents'].$multipart_new_line;
$multipart_body .= '--'.$multipart_boundary.'--'.$multipart_new_line.$multipart_new_line;

$endpoint_url_qs = empty($endpoint_args) ? '' : http_build_query($endpoint_args);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint_url.$endpoint_url_qs);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::ENDPOINT_TIMEOUT);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); # Don't use CURLOPT_PUT (results in a "read timed out" error)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart_body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: multipart/form-data; boundary='.$multipart_boundary,
));

$this->comm_response = curl_exec($ch);
$this->comm_response_info = curl_getinfo($ch);
curl_close($ch);

# ...

gwillmottBH
User
Posts: 75
Joined: Tue Apr 12, 2016 10:34 am

Re: (Solved) Encoding error on attached file (BASE64 NOT DECODED) - 02473109

Postby gwillmottBH » Fri Apr 14, 2017 4:08 pm

Hello Mavieo,

Thank you for confirming this is resolved.
Greg Willmott
Senior Enterprise Support Analyst
B U L L H O R N
Staffing and Recruiting Software, On Target, On Demand
100 Summer St., 17th Floor, Boston, MA 02210
617-478-9126 (US Support)
+44 800 032 2848 ext. 9131 (UK Support)


Return to “REST API”

Who is online

Users browsing this forum: No registered users and 1 guest