I'm working on an integration and I need to create Attachments ( Images as attachments ) for Items. For that, I'm sending an HTTP request to the endpoint /b1s/v1/Attachments2 using PHP CURL ut is corrupted and I'm not able to use it, I've tried using base64 string as well. The same thing happens when I use Postman. The code I'm using
fileContents = file_get_contents('ImageURL');
$imageDataString = "----------------------------117910376810611966486929
Content-Disposition: form-data; name=\"files\"; filename=\"anitest.jpeg\"
Content-Type: image/jpeg
$fileContents
----------------------------117910376810611966486929--";
print_r($imageDataString);
echo "<br>";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => 'https://xx.xx.xx.xx:50000/b1s/v1/Attachments2',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $imageDataString,
CURLOPT_HTTPHEADER => array(
'Content-Type:multipart/form-data; boundary=--------------------------117910376810611966486929',
'Cookie: B1SESSION=' . $cookie->SessionId . '; ROUTEID=.node3',
),
));
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curlError = curl_error($curl);
print_r('HTTP code ' . $httpcode);
echo "<br>";
print_r('error: ' . $curlError);
echo "<br>";
var_dump($response);
curl_close($curl);
However, when I use Postman From-data the attachment is perfectly created without any issues.
Reference:- https://help.sap.com/doc/0d2533ad95ba4ad7a702e83570a21c32/9.3/en-US/Working_with_SAP_Business_One_Service_Layer.pdf Pg. 106
When sending data directly using Postman Form-data SAP receives the file's binary data accordingly. Turns out the Image binary data I'm sending as base64 string is encoded as text/plain and the binary/raw data is treated as application/octet-stream. So, any solution for that?
Any kind of information is appreciated.
Thanks.