Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send multipart/form-data that contains json object and image file via curl? And how can I treat it with Django Rest Framework?

For example I tried that command via terminal:

curl -F "profileImage=@/home/user/image.jpg" -F "{'firstName':'hello'};type=application/json" http://127.0.0.1:8000/api/v1/signup/

Then I received the request object like that:

print request.FILES
# <MultiValueDict: {u'profileImage': [<InMemoryUploadedFile: image.jpg (image/jpeg)>]}>
print request.DATA
# <QueryDict: {u"{'firstName':'hello'};content-type": [u'application/json']}>

The image is ok, but QueryDict is not represented correctly - all JSON file is a key and content-type is a value.

In Django use these parsers:

parser_classes = (MultiPartParser, FormParser, JSONParser,)

I necessary need to send text data via JSON structure .

like image 264
alexche8 Avatar asked May 27 '14 18:05

alexche8


1 Answers

If you want to POST with multipart/form-data content-type, you can't also specify application/json. The simple fix for this is to send the form data in url-encoded format. Try this command:

curl -F "profileImage=@/home/user/image.jpg" -F "firstName=hello" http://127.0.0.1:8000/api/v1/signup/
like image 135
xjtian Avatar answered Sep 22 '22 12:09

xjtian