【问题标题】:What content-type does dropbox (file put) api uses? and How to mimic it?dropbox (file put) api 使用什么内容类型?以及如何模仿它?
【发布时间】:2013-05-10 19:37:10
【问题描述】:

我正在阅读 Dropbox API 的 files_put 文档。

他们使用的 URL 路径是:https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val 并且请求正文包含文件:

required 要上传的文件内容。由于整个 PUT 主体 将被视为文件,任何参数都必须作为的一部分传递 请求网址。请求 URL 应该像您一样签名 签署任何其他 OAuth 请求 URL。

问题

  • 我很想知道这种请求的内容类型是什么? (请求正文中的文件和url字符串中的参数)

  • 如何模仿此 API 功能?特别是在 grails 控制器中。像这样的东西。

  • 如何在 cURL 中测试这种类型的请求 更新:我发现了如何使用 curl here 进行测试。

对于控制器,我设想了这样的东西

  def save () {
    withFormt {
      html {actForHTML}
      <something> {actForREST}
    }
  }

  def actForREST () {
     //how can I get access to the file? I guess url parameters can be accessed by `params`
  }

【问题讨论】:

    标签: java web-services rest grails mime-types


    【解决方案1】:

    REST 控制台无法在请求正文中发送二进制数据。不幸的是,我现在无法访问curl。但我对你的意见很少,我也打算在我的个人机器上尝试。

    • 如何使用 curl 进行文件上传? (@source - cURL 文档)

      4.3 文件上传POST

      早在 1995 年末,他们就定义了另一种通过 HTTP 发布数据的方式。它 记录在 RFC 1867 中,为什么这种方法有时被称为 RFC1867 发布。

      这个方法主要是为了更好的支持文件上传。一种形式 允许用户上传文件的 HTML 可以这样写:

      <form method="POST" enctype='multipart/form-data' action="upload.cgi">
        <input type=file name=upload>
        <input type=submit name=press value="OK">
      </form>
      

      这清楚地表明即将发送的 Content-Type 是 多部分/表单数据。

      要使用 curl 发布到这样的表单,请输入如下命令行:

          curl --form upload=@localfilename --form press=OK [URL]
      
    • W3C 规范

      查看 W3C 规范 here 和 multipat/form-data 的 RFC1867

    • Grails 控制器处理请求

      您的应用应该能够处理multipart/form-data(我认为不需要添加 MIME 类型)。您在控制器中的操作应如下所示:-

    例如:

    def uploadFileAndGetParams(){
        def inputStream = request.getInputStream()
        byte[] buf = new byte[request.getHeaders().CONTENT_LENGTH] //Assuming
        //Read the input stream
        for (int chunk = inputStream.read(buf); chunk != -1; chunk = is.read(buf)){
            //Write it any output stream
            //Can refer the content-type of the file (following W3C spec)
            //and create an Output stream accordingly
        }
    
        //Get the params as well
        //params.foo //params.bar 
    }
    

    它可能不是完整的证明,但它应该没有我想象的那么复杂。我今天也要试试。 Useful post来看看。

    【讨论】:

    • 谢谢!我还在研究这个。但是从我发布的 Dropbox API 链接来看,他们似乎是通过 REST 发送文件的,对吧?因为它说Since the entire PUT body will be treated as the file, any parameters must be passed as part of the request URL. 我也会在我的机器上尝试这种方法。如果你发现了什么,请发帖,我也会这样做。再次感谢!
    • 嘿@Anthony,看看我遇到的这个question。我想你的答案在这里。
    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2015-01-27
    • 2010-12-10
    • 2012-03-03
    • 2015-02-17
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多