7

I am new to django. Can anybody help me... How can I upload a file using the Rest Framework API ?

I have tried following this page:

http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser

3
  • What did you tried atcually? Atleast paste some code. Commented Oct 17, 2013 at 9:26
  • I have tried same code which is given in above link.. Now i am not able to test.. how to test ? Commented Oct 17, 2013 at 11:00
  • @user2709881 check out this blog post Commented Oct 17, 2013 at 14:27

2 Answers 2

13

File uploading in Django REST framework is the same with uploading files in multipart/form in django.

To test it you can use curl:

curl -X POST -H "Content-Type:multipart/form-data" -u {username}:{password} \
-F "{field_name}=@{filename};type=image/jpeg" http://{your api endpoint}

Other fields are just like normal form fields in Django.

Sign up to request clarification or add additional context in comments.

6 Comments

so what if your curl has json data and an image at the same time?
I think you can add those data as form field data as well (in -F parameter). Forgot what I did since I haven't been working on REST framework for a few months now.
Leonid: Did you get the answer of your question. can we json data and image at the same time.
@Leonid check out this code github.com/zheli/duego-photo-gallery. I remember I succeed with this one. You can look into the test for upload in rest_api app.
@Zhe I can't get the test APIClient to upload files - I keep getting a UnicodeDecodeError on the .post call. Did you have to do anything to get this to work?
|
2

Zhe answer is pretty well. Besides, you can add some parameters in order to see the response. Take this one for example:

curl -X PATCH --dump-header - -H "Content-Type:multipart/form-data" -u jorge:123456 -F "image=@/home/oscar/Pictures/dgnest/_MG_6445.JPG;type=image/jpeg" http://localhost:8000/api/project/3/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.