Upload File using Django Rest Framework

2019-03-21 03:39发布

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

2条回答
Deceive 欺骗
2楼-- · 2019-03-21 04:25

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/
查看更多
放我归山
3楼-- · 2019-03-21 04:31

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.

查看更多
登录 后发表回答