title: HTTP POST request and headers with MIME attachments multipart/related and xop?
body: <p>I'm trying to upload a bulk data exchange call to eBay using their FileTransfer API.
  In order to do this, I've got to POST an xml request string to the eBay server but the xml
  request contains a <code>&lt;Data&gt;</code> section that must include the data you're transferring( in this case it's a base64 encoded zipfile containing another xml document). eBay gives an example in C# of how to construct such a document: <a href="https://ebay.custhelp.com/app/answers/detail/a_id/1561" rel="nofollow">https://ebay.custhelp.com/app/answers/detail/a_id/1561</a></p>
  
  <p>I've been trying to recreate this example in python using httplib to POST a string I've constructed in much the same fashion as the example(the 3 UUIDs are unique):</p>
  
  <pre><code>request = """
  --MIMEBoundaryurn_uuid_{XMLUUID}
      Content-Type: application/xop+xml;charset=UTF-8;type="text/xml;charset=UTF-8";
      Content-Transfer-Encoding: binary
      Content-ID:&lt;0.urn:uuid:{REQUUID}&gt;
      &lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;uploadFileRequest xmlns:sct=\"http://www.ebay.com/soaframework/common/types\" xmlns="http://www.ebay.com/marketplace/services"&gt;
      &lt;fileAttachment&gt;
          &lt;Size&gt;{Size}&lt;/Size&gt;
          &lt;Data&gt;&lt;xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include"
          href="cid:urn:uuid:{ATTCHMNTUUID}&gt;"&lt;/Data&gt;
      &lt;/fileAttachment&gt;
      &lt;fileFormat&gt;{fileFormat}&lt;/fileFormat&gt;
      &lt;fileReferenceId&gt;{fileReferenceId}&lt;/fileReferenceId&gt;
      &lt;taskReferenceId&gt;{taskReferenceId}&lt;/taskReferenceId&gt;
      &lt;/uploadFileRequest&gt;
      --MIMEBoundaryurn_uuid_{XMLUUID}
      Content-Type: application/octet-stream
      Content-Transfer-Encoding: binary
      Content-ID: &lt;urn.uuid:{ATTCHMNTUUID}&gt;\r\n
      {Data}
      --MIMEBoundaryurn_uuid_{XMLUUID}--
      """.replace("\t", "")
  
  request_dict = {
      'Size': size,
      'Data': payload,
      'fileFormat': 'zip',
      'fileReferenceId': '50000935383',
      'taskReferenceId': '50000847753',
      'REQUUID': reqUUID,
      'XMLUUID': xmlUUID,
      'ATTCHMNTUUID': attchmntUUID,
  }
  
  
  request = request.format( **request_dict )
  </code></pre>
  
  <p>With headers that look like this:</p>
  
  <pre><code>headers = {
  'X-EBAY-SOA-OPERATION-NAME': 'uploadFile',
  'X-EBAY-SOA-SERVICE-NAME': 'FileTransferService',
  'X-EBAY-SOA-SECURITY-TOKEN': #Auth Token,
  'Content-type': "multipart/related; boundary=" + boundary  + ";type=\"application/xop+xml\";start=\"&lt;0." + "urn:uuid:" + str(requuid) + "&gt;\";start-info=\"text/xml\""
  }
  </code></pre>
  
  <p>and then my POST:</p>
  
  <pre><code>connection = httplib.HTTPSConnection( 'storage.sandbox.ebay.com' )
  connection.request( "POST", '/FileTransferService', request, headers )
  </code></pre>
  
  <p>When I post the xml request without the MIME attachment information it accepts the file with no problems. But when I try to do it like the above code, with MIME multipart/related attachments and the <code>&lt;xop&gt;</code> tag pointing out where the data is located in the attachment, the POST isn't successful and I get a "Error 302: Moved Temporarily" response.  This leads me to believe that something isn't right with how I constructed the MIME multipart/related xml request, or how I constructed the "Content-type" header declaration, or my use of xop, or most likely a combination of all three things.</p>
  
  <p>I guess my question is: How do I create an xml request that contains MIME multipart/related sections and also uses xop?  </p>
  
  <p>Thanks for the help!</p>
  
  <p>Wes</p>