Help with ExternalResourceClient with multipart/form-data?

I need some help with ExternalResourceClient with multipart/form-data? I tried following the examples at https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_12.0/Integration/ExternalResourceClient/ but they don't handle multiipart/form-data.

I need to convert this CURL

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$curl_post_data = [
'input' => $input,
'scriptName' => $scriptName
];
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 3,
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $curl_post_data,
CURLOPT_USERPWD => $credentials,
));
$response = curl_exec($curl);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

When this gets sent to the server the POST variables are sent as Content-Typemultipart/form-data;

The issue I have with the ExternalResourceClient is that expects the Content-Type to be application/x-www-form-urlencoded which the application does not accept.

How do I add a boundary to the $curl_post_data?


Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$erc = new \Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient();
$boundary = '--------------------------'.microtime(true);
$curl_post_data = [
'input' => $input,
'scriptName' => $scriptName
];
$response = $erc->post($url, $curl_post_data, [
'Content-Type' => "multipart/form-data; boundary=$boundary",
'Authorization' => 'Basic '. base64_encode($credentials),
'Accept' => '*/*'
]);
//How do I add boundary to the $curl_post_data?
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Hi ,

    Have you looked into the upload method of the ExternalResourceClient? (here's for reference)

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public function upload(\UploadFile $file, string $url, string $method = 'POST', array $body = [], array $headers = []): ResponseInterface
    {
    $boundary = '--------------------------' . microtime(true);
    $requestHeaders = array_merge(['Content-type' => 'multipart/form-data; boundary=' . $boundary], $headers);
    $fileContents = $file->get_file_contents();
    $content = "--" . $boundary . "\r\n" .
    "Content-Disposition: form-data; name=\"" . $file->field_name . "\"; filename=\"" . basename($file->get_temp_file_location()) . "\"\r\n" .
    "Content-Type: application/zip\r\n\r\n" .
    $fileContents . "\r\n";
    foreach ($body as $name => $value) {
    $content .= "--" . $boundary . "\r\n" .
    "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n" .
    "{$value}\r\n";
    }
    $content .= "--" . $boundary . "--\r\n";
    return $this->request($method, $url, $content, $requestHeaders);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Can you try something like this?

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    $erc = new \Sugarcrm\Sugarcrm\Security\HttpClient\ExternalResourceClient();
    $boundary = '--------------------------'.microtime(true);
    $curl_post_data = [
    'input' => $input,
    'scriptName' => $scriptName
    ];
    $content = "--" . $boundary . "\r\n";
    foreach ($curl_post_data as $name => $value) {
    $content .= "--" . $boundary . "\r\n" .
    "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n" .
    "{$value}\r\n";
    }
    $content .= "--" . $boundary . "--\r\n";
    $response = $erc->post($url, $content, [
    'Content-Type' => "multipart/form-data; boundary=$boundary",
    'Authorization' => 'Basic '. base64_encode($credentials),
    'Accept' => '*/*'
    ]);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    SugarCRM | Principal Developer Advocate

  • I will give it a try.

    can you submit an Idea as this is a common use case

  • Hi ,

    Sure, let me know if it works and I'll update our documentation accordingly with this example,

    Appreciate it.

    SugarCRM | Principal Developer Advocate