<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to call Blob.Attach via REST API in Python? in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-call-blob-attach-via-rest-api-in-python/m-p/328104#M15105</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;This worked for me:&lt;/P&gt;
&lt;P&gt;1: Go and clone sfermigier &lt;A href="https://bitbucket.org/sfermigier/nuxeo-automation-clients/src/2bc643c4c040895b50c625a485171d480483832e/python/nuxeoautomation.py?at=default"&gt;source&lt;/A&gt; as suggested in &lt;A href="http://doc.nuxeo.com/display/NXDOC56/Using+a+Python+client"&gt;Rest API documentation&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2: Rewrite this method&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; def _attach_blob(self, blob, **params):
    filename = os.path.basename(blob)

    container = MIMEMultipart("related",
            type="application/json+nxrequest",
            start="request")

    d = {'params': params}
    json_data = json.dumps(d)
    json_part = MIMEBase("application", "json+nxrequest")
    json_part.add_header("Content-ID", "request")
    json_part.set_payload(json_data)
    container.attach(json_part)

    ctype, encoding = mimetypes.guess_type(filename)
    if ctype:
        maintype, subtype = ctype.split('/', 1)
    else:
        maintype, subtype = "application", "binary"
    blob_part = MIMEBase(maintype, subtype)
    blob_part.add_header("Content-ID", "input")
    blob_part.add_header("Content-Transfer-Encoding", "binary")
    blob_part.add_header("Content-Disposition",
        "attachment;filename=%s" % filename)

    blob_part.set_payload(open(blob,"rb").read())
    container.attach(blob_part)

    # Create data by hand :(
    boundary = "====Part=%s=%s===" % (time.time, random.randint(0, 1000000000))
    headers = {
            "Accept": "application/json+nxentity, */*",
            "Authorization": self.auth,
            "Content-Type": 'multipart/related;boundary="%s";type="application/json+nxrequest";start="request"'
                % boundary,
    }
    data = "--" + boundary + "\r\n" \
            + json_part.as_string() + "\r\n" \
            + "--" + boundary + "\r\n" \
            + blob_part.as_string() + "\r\n" \
            + "--" + boundary + "--"

    req = urllib2.Request(self.root + "Blob.Attach", data, headers)
    try:
        resp = self.opener.open(req)
    except Exception, e:
        self._handle_error(e)
        raise

    s = resp.read()
    return s
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3: Call the method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;session = Session(operationURI, user, password)
session._attach_blob('c:\\hop.pdf', document=aDocID)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where aDocID is the UID of the document you want to fill in.&lt;/P&gt;
&lt;P&gt;Notice that I may just not have found the proper way to make it work as given but my binary attachment went its way like this.&lt;/P&gt;</description>
    <pubDate>Wed, 02 Oct 2013 17:39:03 GMT</pubDate>
    <dc:creator>Antoine_Cordier</dc:creator>
    <dc:date>2013-10-02T17:39:03Z</dc:date>
    <item>
      <title>How to call Blob.Attach via REST API in Python?</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-call-blob-attach-via-rest-api-in-python/m-p/328103#M15104</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I am trying to use Python to POST data to the Nuxeo Automation API. Unfortunately I am unable to compose the "multipart/related" portion of the message correctly -- I always receive the message "Failed to parse multipart request" with cause "Missing start boundary."&lt;/P&gt;
&lt;P&gt;Here is the code I've extracted:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/empiricalthought/c00eb95ec35f892a628b"&gt;https://gist.github.com/empiricalthought/c00eb95ec35f892a628b&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Here is what the output looks like when sent to netcat:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/empiricalthought/384ae7bbef10992b75b7"&gt;https://gist.github.com/empiricalthought/384ae7bbef10992b75b7&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This looks similar enough to the output at &lt;A href="http://doc.nuxeo.com/display/NXDOC/REST+API" target="test_blank"&gt;http://doc.nuxeo.com/display/NXDOC/REST+API&lt;/A&gt; that I am stumped.&lt;/P&gt;
&lt;P&gt;Thank you for any help.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Apr 2013 15:45:54 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-call-blob-attach-via-rest-api-in-python/m-p/328103#M15104</guid>
      <dc:creator>Steven_Huwig1</dc:creator>
      <dc:date>2013-04-03T15:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to call Blob.Attach via REST API in Python?</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-call-blob-attach-via-rest-api-in-python/m-p/328104#M15105</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;This worked for me:&lt;/P&gt;
&lt;P&gt;1: Go and clone sfermigier &lt;A href="https://bitbucket.org/sfermigier/nuxeo-automation-clients/src/2bc643c4c040895b50c625a485171d480483832e/python/nuxeoautomation.py?at=default"&gt;source&lt;/A&gt; as suggested in &lt;A href="http://doc.nuxeo.com/display/NXDOC56/Using+a+Python+client"&gt;Rest API documentation&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2: Rewrite this method&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt; def _attach_blob(self, blob, **params):
    filename = os.path.basename(blob)

    container = MIMEMultipart("related",
            type="application/json+nxrequest",
            start="request")

    d = {'params': params}
    json_data = json.dumps(d)
    json_part = MIMEBase("application", "json+nxrequest")
    json_part.add_header("Content-ID", "request")
    json_part.set_payload(json_data)
    container.attach(json_part)

    ctype, encoding = mimetypes.guess_type(filename)
    if ctype:
        maintype, subtype = ctype.split('/', 1)
    else:
        maintype, subtype = "application", "binary"
    blob_part = MIMEBase(maintype, subtype)
    blob_part.add_header("Content-ID", "input")
    blob_part.add_header("Content-Transfer-Encoding", "binary")
    blob_part.add_header("Content-Disposition",
        "attachment;filename=%s" % filename)

    blob_part.set_payload(open(blob,"rb").read())
    container.attach(blob_part)

    # Create data by hand :(
    boundary = "====Part=%s=%s===" % (time.time, random.randint(0, 1000000000))
    headers = {
            "Accept": "application/json+nxentity, */*",
            "Authorization": self.auth,
            "Content-Type": 'multipart/related;boundary="%s";type="application/json+nxrequest";start="request"'
                % boundary,
    }
    data = "--" + boundary + "\r\n" \
            + json_part.as_string() + "\r\n" \
            + "--" + boundary + "\r\n" \
            + blob_part.as_string() + "\r\n" \
            + "--" + boundary + "--"

    req = urllib2.Request(self.root + "Blob.Attach", data, headers)
    try:
        resp = self.opener.open(req)
    except Exception, e:
        self._handle_error(e)
        raise

    s = resp.read()
    return s
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3: Call the method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;session = Session(operationURI, user, password)
session._attach_blob('c:\\hop.pdf', document=aDocID)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where aDocID is the UID of the document you want to fill in.&lt;/P&gt;
&lt;P&gt;Notice that I may just not have found the proper way to make it work as given but my binary attachment went its way like this.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2013 17:39:03 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-call-blob-attach-via-rest-api-in-python/m-p/328104#M15105</guid>
      <dc:creator>Antoine_Cordier</dc:creator>
      <dc:date>2013-10-02T17:39:03Z</dc:date>
    </item>
  </channel>
</rss>

