java - dropbox (file put) api 使用什么内容类型?以及如何模仿它?

标签 java web-services rest grails mime-types

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

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

required The file contents to be uploaded. Since the entire PUT body will be treated as the file, any parameters must be passed as part of the request URL. The request URL should be signed just as you would sign any other OAuth request URL.

问题

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

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

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

我设想的 Controller 是这样的

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

最佳答案

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 规范 hereRFC1867用于 multipat/form-data

  • Grails Controller 处理请求

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

例如:

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看看。

关于java - dropbox (file put) api 使用什么内容类型?以及如何模仿它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16578475/

相关文章:

X.509 安全 Web 服务的 Java 客户端

java.lang.IllegalStateException : Configuration error: found multiple declarations of @BootstrapWith for test class 错误

java - “字段需要一个无法找到的 bean。”使用mongodb的错误spring restful API

Java - 理解克隆

java - 在 android 上使用 Socket.IO 总是返回 XHR 轮询错误

java - 仅当队列中有内容时才消耗 cpu 的工作线程

java - 返回数组的类

java - 如何使用jboss部署wsdl

java - SOAP 错误 : Axis 2 AbstractMethodError

.net - RestSharp 压缩请求,同时对服务器进行休息调用