I have been working for days on this with no luck, when trying to create an entry with a PDF attachment.
Using Remedy REST API.
I get a timeout error (500) or a Bad Reques every time.
Here is the documentation: https://docs.bmc.com/docs/ars2002/example-of-using-the-rest-api-to-create-an-entry-on-a-form-909638132.html#post-attachments-341682506 and a Java example.
I am using XAMPP on Windows. PHP 7 and Curl Version 7.70.
Do I have something wrong with my CURL call?
<?php
$urlSample = '<<MY URL>>';
$myUser = '<<MY USER>>';
$myPassword = '<<MY PASS>>';
$myAuthString = '<<MY AUTH STRING>>';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $urlSample . '/api/jwt/login?username='.$myUser.'&password='.$myPassword.'&authString='.$myAuthString,
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_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
$token = $response; // This WORKS!!!
// Define the endpoint URL
$url = $urlSample . '/api/arsys/v1/entry/SRM:RequestInterface_Create?fields=values(Request Number)';
// Define the PDF attachment
$pdf_file = "mypdf.pdf";
// Define the form fields and their values
$fields = array(
"values" => array(
array(
"z1D Action" => "CREATE",
"Source Keyword" => "RESTAPI",
"TitleInstanceID" => "<< MY INSTANCE ID >>",
"Login ID" => $myUser,
"Customer Login" => "example",
"Date Required" => date('Y-m-d') . 'T' . date('h:m:s'),
"z1D_WorkInfoSummary" => "PDF Form", // It Works When I don't send an Attachment
"z2AF_WIAttachment1" => $pdf_file,
"SR Type Field 1" => "Requestor Name:TEST | Phone Number:123-456-7890 | Equipment: Email",
"SR Type Field 2" => "TEST",
"SR Type Field 3" => "TEST",
"SR Type Field 4" => "TEST",
"SR Type Field 5" => "TEST",
)
)
);
// Create the multipart/form-data boundary
$boundary = uniqid();
// Build the multipart/form-data payload
$data = "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"entry\"\r\n";
$data .= "Content-Type: application/json\r\n\r\n";
$data .= json_encode($fields) . "\r\n";
$data .= "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"attach-z2AF_WIAttachment1\"; filename=\"$pdf_file\"\r\n";
$data .= "Content-Type: application/octet-stream\r\n\r\n";
$data .= file_get_contents($pdf_file) . "\r\n";
$data .= "--" . $boundary . "--\r\n";
// Define the HTTP headers
$headers = array(
"Authorization: AR-JWT " . $token,
"Content-Type: multipart/form-data; boundary=" . $boundary,
"Accept: application/json"
);
// Initialize cURL
$curl = curl_init();
// Set the cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($curl); // Got a Timeout
// Close the cURL session
curl_close($curl);
// Print the response
print_r(json_decode($response));
?>
CodePudding user response:
I was able to make it work on PHP after a lot of struggling using this:
<?php
// Define the endpoint URL
$url = '<< MY URL >>';
// Define the PDF attachment
$pdf_file = "C:\\xampp\\htdocs\\curl-tests\\test.pdf";
// Define the form fields and their values
$fields = array(
"values" => array(
array(
"z1D Action" => "CREATE",
"Source Keyword" => "RESTAPI",
"TitleInstanceID" => "<< MY INSTANCE ID>>",
"Login ID" => "<< MY USER >>",
"Customer Login" => "jactest",
"Date Required" => date('Y-m-d') . 'T' . date('h:m:s'),
"z1D_WorkInfoSummary" => "TEST",
"z2AF_WIAttachment1" => $pdf_file,
"SR Type Field 1" => "TEST",
"SR Type Field 2" => "TEST",
"SR Type Field 3" => "TEST",
"SR Type Field 4" => "TEST",
"SR Type Field 5" => "TEST",
)
)
);
// Create the multipart/form-data boundary
$boundary = uniqid();
// Build the multipart/form-data payload
$data = "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"entry\"\r\n";
$data .= "Content-Type: application/json\r\n\r\n";
$data .= str_replace(']','',str_replace('[','',json_encode($fields))) . "\r\n";
// $data .= json_encode($fields) . "\r\n"; // NOT GOOD
$data .= "--" . $boundary . "\r\n";
$data .= "Content-Disposition: form-data; name=\"attach-z2AF_WIAttachment1\"; filename=\"$pdf_file\"\r\n";
$data .= "Content-Type: application/octet-stream\r\n\r\n";
$data .= file_get_contents($pdf_file) . "\r\n";
$data .= "--" . $boundary . "--\r\n";
// echo nl2br($data); exit();
// Define the HTTP headers
$headers = array(
"Authorization: AR-JWT " . $token,
"Content-Type: multipart/form-data; boundary=" . $boundary,
"Content-Length: " . strlen($data),
);
// Set up the cURL options
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => fopen('logs_remedy.txt', 'w'),
);
// Initialize cURL
$curl = curl_init();
curl_setopt_array($curl, $options);
// Execute the request
$response = curl_exec($curl);
// Close the cURL session
curl_close($curl);
// Print the response
$theResponse = (json_decode($response));
echo '<b>RESPONSE WITH FILE STREAM</b><br>';
echo '<pre>',print_r($theResponse),'</pre>';
?>
Notice the json I generated, it was my main issue, I had to validate it several times and came up with some "[" "]" chars that I had to remove and it finally worked.