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

$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);

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?


$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?

Parents
  • Hi ,

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

    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);
    }

    Can you try something like this?

    $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' => '*/*'
    ]);
    

    SugarCRM | Principal Developer Advocate

  • I will give it a try.

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

Reply Children