title: best approach to design a rest web service with binary data to be consumed from the browser
body: <p>I'm developing a json rest web service that will be consumed from a single web page app built with backbone.js</p>
  
  <p>This API will let the consumer upload files related to some entity, like pdf reports related to a project</p>
  
  <p>Googling around and doing some research at stack overflow I came with these possible approaches:</p>
  
  <p><strong>First approach:</strong> <strong>base64 encoded data field</strong></p>
  
  <pre><code>POST: /api/projects/234/reports
  {
    author: 'xxxx',
    abstract: 'xxxx',
    filename: 'xxxx',
    filesize: 222,
    content: '&lt;base64 encoded binary data&gt;'
  }
  </code></pre>
  
  <p><strong>Second approach: multipart form post:</strong></p>
  
  <pre><code>POST: /api/projects/234/reports
  {
    author: 'xxxx',
    abstract: 'xxxx',
  }
  </code></pre>
  
  <p>as a response I'll get a report id, and with that I shall issue another post</p>
  
  <pre><code>POST: /api/projects/234/reports/1/content
  enctype=multipart/form-data
  </code></pre>
  
  <p>and then just send the binary data</p>
  
  <p>(have a look at this: <a href="https://stackoverflow.com/a/3938816/47633">https://stackoverflow.com/a/3938816/47633</a>)</p>
  
  <p><strong>Third approach: post the binary data to a separate resource and save the href</strong></p>
  
  <p>first I generate a random key at the client and post the binary content there</p>
  
  <pre><code>POST: /api/files/E4304205-29B7-48EE-A359-74250E19EFC4
  enctype=multipart/form-data
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>POST: /api/projects/234/reports
  {
    author: 'xxxx',
    abstract: 'xxxx',
    filename: 'xxxx',
    filesize: 222,
    href: '/api/files/E4304205-29B7-48EE-A359-74250E19EFC4'
  }
  </code></pre>
  
  <p>(see this: <a href="https://stackoverflow.com/a/4032079/47633">https://stackoverflow.com/a/4032079/47633</a>)</p>
  
  <p>I just wanted to know if there's any other approach I could use, the pros/cons of each, and if there's any established way to deal with this kind of requirements</p>
  
  <p>the big con I see to the first approach, is that I have to fully load and base64 encode the file on the client</p>
  
  <p><strong>some useful resources:</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4032032/post-binary-data-to-a-restful-application">Post binary data to a RESTful application</a></li>
  <li><a href="https://stackoverflow.com/questions/1509743/what-is-a-good-way-to-transfer-binary-data-to-a-http-rest-api-service">What is a good way to transfer binary data to a HTTP REST API service?</a></li>
  <li><a href="https://stackoverflow.com/questions/3938569/how-do-i-upload-a-file-with-metadata-using-a-rest-web-service?rq=1">How do I upload a file with metadata using a REST web service?</a></li>
  <li><a href="https://stackoverflow.com/questions/1500744/bad-idea-to-transfer-large-payload-using-web-services">Bad idea to transfer large payload using web services?</a></li>
  <li><a href="https://stackoverflow.com/a/5528267/47633">https://stackoverflow.com/a/5528267/47633</a></li>
  </ul>