type does dropbox (file put) api uses? and How to mimic it?
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.
链接地址: http://www.djcxy.com/p/48648.html