upload image to imagefield with djangorestframework using json and test this with CURL
Asked Answered
D

1

8

I have made several apis in djangorestframework. This I could test both with the html form of the api as with curl in commandline.

Now I have an api to a Model with one off the fields an ImageField.

I can't figure out which curl command to use. Using the syntax I used before fot post actions in json format, it would be:

curl -X POST -S -H 'Content-Type: application/json' -u "username:password" --data-binary '{"otherfields":"something", "photo":"/home/michel/test.jpg"}' 127.0.0.1:8000/api/v1/

but in this case the photo will not be saved and left empty (the photo is an optional field)

adding -T /home/michel/test.jpg I get an error message saying 127.0.0.1:800/api/v1/test.jpg does not exist as an url.

In the test html form of djangorestframework, all works fine.

Using the -F option, it says I can only do 1 request at a time... I also removed the datatype from data-binary

Can anybody help me how to make this curl post with both the image and the other data in json in 1 command.

Downer answered 9/7, 2012 at 15:52 Comment(0)
D
11

After a long puzzle, this seems to do the trick:

  • put all json arguments in separate -F arguments
  • only use the header Accept (not Content-Type)
  • And specify the image type
  • Use @ to indicate the local file to upload

    curl -X POST -S \
      -H 'Accept: application/json' \
      -u "username:password" \
      -F "otherfields=something" \
      -F "photo=@/home/michel/test.jpg;type=image/jpg" \
      http://127.0.0.1:8000/api/v1/
    

By the way, I know all of this is on the documentation site of curl, but just missed an example of all those things together since there are a lot of options to try out.

Downer answered 13/7, 2012 at 8:32 Comment(3)
What am I doing wrong? $ curl -X POST -S -H "Accept-Language: en-en" -H 'Accept: application/json' \ -F "name=Avatar" \ -F "username=avatar" \ -F "password=A1234567" \ -F "[email protected]" \ -F "token=748e14e32ce165d06365d3c9a00cee3f" \ -F "avatar=@/home/moreno/Pictures/avatar004.jpg;type=image/jpg" 127.0.0.1:8000/api/v1/users/register/ response 400 {"username":["This field is required."],"password":["This field is required."],"name":["This field is required."],"avatar":["No file was submitted."],"email":["This field is required."]}Margitmargo
it is missing one of the reuired fields. You need to use -u in front of username, not -F (unless it's also a field in the api, than you need to use both)Downer
@michel-iamit thanks. I decided to send the images as a base64 field e.g.: curl -H "Authorization: JWT ey...5U" -d "name=User Name&username=user&[email protected]&password=A1234567" --data-urlencode 'avatar='"$( base64 ~/Pictures/avatar004.jpg)"'' --data-urlencode 'image='"$( base64 ~/Pictures/avatar001.jpg)"'' --data-urlencode 'picture='"$( base64 ~/Pictures/577383.jpg)"'' http://localhost:8000/api/v1//register/Margitmargo

© 2022 - 2024 — McMap. All rights reserved.