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

前端 未结 1 1986
醉酒成梦
醉酒成梦 2020-12-31 19:22

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

相关标签:
1条回答
  • 2020-12-31 20:03

    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.

    0 讨论(0)
提交回复
热议问题