2

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

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

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

问题

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

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

  • 如何在cURL Update中测试这种类型的请求:我在这里找到了如何使用 curl 进行测试。

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

  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`
  }
4

1 回答 1

1

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

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

    4.3 文件上传POST

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

    该方法主要是为了更好地支持文件上传而设计的。允许用户上传文件的表单可以用 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 是 multipart/form-data。

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

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

    看看这里的 W3C 规范和RFC1867的 multipat/form-data

  • 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 
}

它可能不是完整的证据,但它应该没有我想象的那么复杂。我今天也要试试。有用的帖子看看。

于 2013-05-16T18:10:00.297 回答