Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:19:06+00:00 2026-05-31T10:19:06+00:00

I need to submit an XML and an image together to a restful web

  • 0

I need to submit an XML and an image together to a restful web service. Is this possible using curl ?

Of the various examples available in web, I could see that they are passing a multipart and other form data together. But, I could not locate an example which sends an XML and an image together.

Here is the code I am trying.

<?php
$url = "http://www.myservice.com/event";

$file = dirname(__FILE__) . "\image.jpg"; 
$boundary  = "---------------------".substr(md5(rand(0,32000)), 0, 10);  

$event_xml_data =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' .
'<ns5:event ' .
'xmlns="http://www.myservice.com/Common/"' .
'xmlns:ns2="http://www.myservice.com/Event/"' .
'xmlns:ns3="http://www.myservice.com/Media/"' .
'xmlns:ns4="http://www.myservice.com/Media/" ' .
'xmlns:ns5="http://www.myservice.com/Event/"> ' .
'<id>0</id>' .
'<name>Event2004</name> '.
'<ns2:description>Description845</ns2:description>' .
'<ns2:startDate>2012-02-27T22:21:29.458-06:00</ns2:startDate>' .
'<ns2:owner>Madhu</ns2:owner>' .
'</ns5:event>';

$content_type_header = 'Content-Type: multipart/form-data; '
    .'type="application/xml"; '
    .'boundary="' . $boundary . '"; '
    .'start="<event>"; '
    .'start-info="application/xml";';
$accept_header = 'Accept: multipart/form-data';
$transfer_encoding_header = 'Transfer-Encoding: chunked';

$fileContents = file_get_contents($file);

$headers = array($content_type_header, $accept_header, $transfer_encoding_header);

$postData  = $boundary . "\r\n"
        ."Content-Type: application/xml\r\n\r\n"
     ."Content-Transfer-Encoding: binary\r\n\r\n"
        ."Content-ID: <event>\r\n\r\n"
        .$event_xml_data . "\r\n"
        .$boundary . "\r\n"
        ."Content-Type: image/jpeg\r\n"
        ."Content-Transfer-Encoding: binary\r\n\r\n"
        . $fileContents . "\r\n"
        .$boundary . "--\r\n";

$postDataNoMedia  = $boundary . "\r\n"
        ."Content-Type: application/xml\r\n\r\n"
     ."Content-Transfer-Encoding: binary\r\n\r\n"
        ."Content-ID: <event>\r\n\r\n"
        .$event_xml_data . "\r\n"
        .$boundary . "--\r\n";

$f = fopen('request.txt', 'w');
//open connection
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'madhukampurath:xxxxxx');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postDataNoMedia);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_VERBOSE  ,1);
curl_setopt($ch, CURLOPT_STDERR  , $f);

//execute post
$output = curl_exec($ch);


$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE); 

fclose($f);

echo $http_code . "<br />";

echo $output; 

echo $error; 

curl_close($ch); 

?>

Web service is done in java. Tomcat logs give this error.

org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: WebApplicationException has been caught : Couldn't find MIME boundary: -----------------------8a09e81751

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:19:08+00:00Added an answer on May 31, 2026 at 10:19 am

    I could solve the issue with the help of my friend Gerald Cantor. I am posting the answer below.

    It was because of a badly formed request. The boundaries were not defined correctly. Changed section is indicated below.

    <?php
    $url = "http://www.myservice.com/event";
    
    $file = dirname(__FILE__) . "\image.jpg"; 
    $boundary  = "---------------------".substr(md5(rand(0,32000)), 0, 10);  
    
    $event_xml_data =
    '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' .
    '<ns5:event ' .
    'xmlns="http://www.myservice.com/Common/"' .
    'xmlns:ns2="http://www.myservice.com/Event/"' .
    'xmlns:ns3="http://www.myservice.com/Media/"' .
    'xmlns:ns4="http://www.myservice.com/Media/" ' .
    'xmlns:ns5="http://www.myservice.com/Event/"> ' .
    '<id>0</id>' .
    '<name>Event2004</name> '.
    '<ns2:description>Description845</ns2:description>' .
    '<ns2:startDate>2012-02-27T22:21:29.458-06:00</ns2:startDate>' .
    '<ns2:owner>Madhu</ns2:owner>' .
    '</ns5:event>';
    
    $content_type_header = 'Content-Type: multipart/form-data; '
        .'type="application/xml"; '
        .'boundary="' . $boundary . '"; '
        .'start="<event>"; '
        .'start-info="application/xml";';
    $accept_header = 'Accept: multipart/form-data';
    $transfer_encoding_header = 'Transfer-Encoding: chunked';
    
    $fileContents = file_get_contents($file);
    
    $headers = array($content_type_header, $accept_header, $transfer_encoding_header);
    
    // Here is the change.
    $postData  = "--" . $boundary . "\r\n"
                ."Content-Type: application/xml\r\n"
                ."Content-Transfer-Encoding: binary\r\n"
                ."Content-ID: <event>\r\n\r\n"
                .$event_xml_data . "\r\n"
                ."--" .$boundary . "\r\n"
                ."Content-Type: image/jpeg\r\n"
                ."Content-Transfer-Encoding: binary\r\n"
                ."Content-ID: <media>\r\n\r\n"
                . $fileContents . "\r\n"
                ."--" .$boundary . "--";
    
    $postDataNoMedia  = "--" . $boundary . "\r\n"
                ."Content-Type: application/xml\r\n"
                ."Content-Transfer-Encoding: binary\r\n"
                ."Content-ID: <event>\r\n\r\n"
                .$event_xml_data . "\r\n"
                ."--" .$boundary . "--";
    // Change ends.
    
    $f = fopen('request.txt', 'w');
    //open connection
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'madhukampurath:xxxxxx');
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postDataNoMedia);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_HEADER,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
    curl_setopt($ch, CURLOPT_VERBOSE  ,1);
    curl_setopt($ch, CURLOPT_STDERR  , $f);
    
    //execute post
    $output = curl_exec($ch);
    
    
    $error = curl_error($ch);
    $http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE); 
    
    fclose($f);
    
    echo $http_code . "<br />";
    
    echo $output; 
    
    echo $error; 
    
    curl_close($ch); 
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading an xml file using javascript and then I need to submit
I need some help posting some XML to a WCF service. In essence, the
I am currently writing an web application where you need to log in, using
How can I access this element: <input type=submit value=Save as XML onclick=some code goes
I need to submit data from a form to an SQL2005 database via a
I need to submit a hash of attributes directly from the form tag. Is
I need to submit a college project proposal for a dbms on airline reservation
I need the button submit to be disabled unless JavaScript is on. I tried:
Need the three Options to be hyperlinks that submit the choice form. When submitted,
I need a Java class to submit tickets to BMC Remedy's Helpdesk product. Wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.