Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to POST 2 files in a single HTTP Request to a REST API.
File 1 will contain data in XML format.
File 2 will be MS word (.doc) document.

Please, guide how to post 2 files in a single multipart request.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-15 17:03pm    
So, Perl or PHP or Rubi, or JavaScript (client-side or server-side)? Which code should send the request and have files, client-side or server-side?
—SA
Mohibur Rashid 2-Apr-15 4:08am    
HTML5 is capable of sending multiple files. PHP is also capable of receiving multiple file. i don't know about perl or ruby. By the way, do you know whatever you are doing? Your tagging is suggesting otherwise.
xprtguro 5-Apr-15 6:37am    
I am able to send 2 files in a single Http request using Node.js.
But facing issues in PHP. Here is my code:

$data = array( 'file2'=>'@C:/temp/Test_1.doc;filename=Test_1.doc',
'file1'=>'@C:/temp/Tes_1.xml;filename=Test_1.xml'
);

$mydata = http_build_query($postfields);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $signedURI);
$page = curl_exec($ch);
$header = curl_getinfo( $ch );
print_r($page);


any idea?

1 solution

I am able to do it in PHP as well. Here is the Code:

I have PHP 5.6.3

PHP
$data1 = array(  'file2' => new CurlFile('C:/temp/Test_1.doc', 'application/msword'),
                'file1' => new CurlFile('C:/temp/Test_1.xml', 'text/xml')
        );

$mydata = http_build_query($postfields);

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_URL, $signedURI);

$responseStream = curl_exec($ch);
$header = curl_getinfo( $ch );

if ($header['http_code'] == 200)
     print_r("File uploaded successfully");
     saveFile($responseStream, $outPath); // your save file to disk logic
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900