Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload File using Django Rest Framework

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

like image 913
user2709881 Avatar asked Oct 17 '13 09:10

user2709881


People also ask

What is JSONParser Django?

JSONParser. Parses JSON request content. request. data will be populated with a dictionary of data. .media_type: application/json.

Can we use Django for REST API?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

What is renderers in Django REST Framework?

Renderers are used to serialize the response into a specific media type like JSON, XML, YAML, etc. Django REST Framework provides various built-in renderer classes and it also supports to write a custom renderer.


2 Answers

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.

like image 134
Zhe Li Avatar answered Oct 04 '22 01:10

Zhe Li


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/
like image 26
oskargicast Avatar answered Oct 04 '22 02:10

oskargicast