Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload files throught POST. #31

Open
nilp0inter opened this issue May 8, 2014 · 6 comments
Open

Upload files throught POST. #31

nilp0inter opened this issue May 8, 2014 · 6 comments

Comments

@nilp0inter
Copy link

Hi,

Is it possible to upload a file using the POST method of the resources?

Something like:

myapi.myresource.post(params={'foo': 1},
                      data=open('bar.raw', 'r').read())

Thank you.

@derks
Copy link
Member

derks commented May 12, 2014

Short answer, dRest doesn't support file uploads out of the box.

I've not interfaced with an API that accepts file uploads, however it is entirely possible. You simply need to sub-class RESTResourceHandler and override the post function to handle the data upload. .. potentially also need to sub-class RequestHandler as well.

http://drest.readthedocs.org/en/latest/customization.html
http://drest.readthedocs.org/en/latest/api.html#module-drest.resource
http://drest.readthedocs.org/en/latest/api.html#drest-request

I could potentially help with this, however I would need an API example to work with to really know what the API is expecting.

@nilp0inter
Copy link
Author

I'll give it a try. Thank you!

If you need an API example you can use the excellent httpbin.org.
Example:

curl -i -F name=test -F [email protected] http://httpbin.org/post
  • name: Is a post parameter with value test.
  • filedata: Is the filename of the first uploaded file. The content is ./README.md
  • -i: Option is for curl to display the content back.

httpbin.org will return a JSON with all the uploaded contents.

After playing with this I think that a more correct interface could be:

myapi.myresource.post(params={'foo': 1},
                      files={'bar.raw': open('bar.raw', 'r').read()})

Because the name of the file is mandatory and HTTP allows you to send multiple files at once.

@derks
Copy link
Member

derks commented Jun 17, 2014

@nilp0inter thanks for the followup, and sorry for the delay in my response. As mentioned, I think the best approach here is to customize dRest to work how you need it to for this specific purpose. Have you had any luck with that, or do you still need some help with it?

@nilp0inter
Copy link
Author

Hi @derks ,

unfortunately I couldn't work on it because I've been very busy. Nevertheless I'm still interested in making the patch.

I seen the mentioned code and it doesn't look so difficult how I thought in the beginning.

I'll be back with a patch soon.

Thank you!

@keithjjones
Copy link

Any luck on this issue?

@nilp0inter
Copy link
Author

@keithjjones , sorry I never make any progress in the issue and I never will, because we stopped using the library as a dependency in our project (nothing wrong with the library, but our requirements changed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants