Dropbox(文件放)api使用什么内容类型?和如何模仿它? [英] What content-type does dropbox (file put) api uses? and How to mimic it?

查看:143
本文介绍了Dropbox(文件放)api使用什么内容类型?和如何模仿它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

他们使用的网址路径是: https://api-content.dropbox.com/1/files_put/<root>/< ;路径>?param = val 并且请求主体保存文件:


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


strong>




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

  • API函数如何模仿?特别是在Grails控制器中。像这样的东西。

  • 我发现如何用curl 这里进行测试。


对于我所设想的控制器而言,

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

def actForREST(){
//我怎样才能访问文件?我猜url参数可以通过`params`
}


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


  • 如何使用卷曲文件上传? (@source - cURL docs)

    4.3文件上传POST



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



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

     < form method =POSTenctype ='multipart / form-data'action =upload.cgi> 
    <输入类型=文件名称=上传>
    < input type = submit name = press value =OK>
    < / form>

    这清楚地表明即将发送的Content-Type是
    multipart / form-数据。



    要使用curl发布到这种形式,请输入如下命令行:

      curl --form upload = @ localfilename --form press = OK [URL] 


  • W3C规范

    查看W3C规范这里 RFC1867 for multipat / form-data


  • Grails控制器处理请求

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




例如:

  def uploadFileAndGetParams(){
def inputStream = request.getInputStream()
byte [] buf = new byte [request.getHeaders()。 (int buf); chunk!= -1; chunk = is.read(buf)){$ b $; //假设
//读取输入流
(int chunk = inputStream.read(buf) b //将它写入任何输出流
//可以引用文件的内容类型(遵循W3C规范)
//并相应地创建一个输出流
}

//获取参数以及
//params.foo //params.bar
}

这可能不是充分的证据,但应该比我想象的要复杂。今天我会尝试一样的。 有用的帖子查看。


I was reading the files_put documentation for the Dropbox API.

The URL Path they use is: https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val and request body holds the file:

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.

Questions

  • I am curious to know what is the content-type of this type of request? (file in request body and parameters in url string)

  • How can this API functionality be mimics? specifically in a grails controller. Something like this.

  • How would this type of request be tested in cURL Update : I found out how to test this with curl here.

For the controller I envisioned something like this

  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 console does not have the ability to send binary data in request body. Unfortunately, I cannot access curl right now. But I have few inputs for you, and I am also going to try the same in my personal machine.

  • How to use curl for file upload? (@source - cURL docs)

    4.3 File Upload POST

    Back in late 1995 they defined an additional way to post data over HTTP. It is documented in the RFC 1867, why this method sometimes is referred to as RFC1867-posting.

    This method is mainly designed to better support file uploads. A form that allows a user to upload a file could be written like this in HTML:

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

    This clearly shows that the Content-Type about to be sent is multipart/form-data.

    To post to a form like this with curl, you enter a command line like:

        curl --form upload=@localfilename --form press=OK [URL]
    

  • W3C Specification

    Have a look at the W3C Spec here and the RFC1867 for multipat/form-data

  • Grails Controller to handle request

    Your app should be able to handle the multipart/form-data(no MIME type addition should be required, I think). Your action in the controller should look like below:-

For example:

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 
}

It may not be full proof but it should be less complicated than what I thought it would be. I am going to try the same today. Useful post to look at.

这篇关于Dropbox(文件放)api使用什么内容类型?和如何模仿它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆