使用json和djangorestframework将图像上传到ima​​gefield并使用CURL测试此 [英] upload image to imagefield with djangorestframework using json and test this with CURL

查看:302
本文介绍了使用json和djangorestframework将图像上传到ima​​gefield并使用CURL测试此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在djangorestframework中做了几个apis。
我可以使用api的html形式测试这两者,就像curl在命令行中一样。

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

现在我有一个api到一个模型一个ImageField。

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

我无法弄清楚使用哪个curl命令。
使用我在fot post操作之前使用的json格式的语法,它将是:

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)

添加 -T /home/michel/test.jpg
我得到一个错误消息,说127.0.0.1:800/api/v1/test.jpg不存在作为一个url。

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.

在测试html形式djangorestframework,一切正常。

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

使用 -F 选项,它说我只能做一个请求时间...
我也从data-binary中删除了数据类型

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

任何人都可以帮助我如何使这个curl post与图像和其他数据

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

推荐答案

在一个长长的谜题之后,这似乎做的诀窍:

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


  • 将所有json参数放在单独的-F项中

  • 只使用标题Accept(not Content-Type)

  • 并指定图片类型

  • 使用@表示要上传的区域设置文件

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

curl -X POST -S -H'Accept:application / json'-uusername:password
-Fotherfields = something-Fphoto = @ / home / michel / test.jpg; type = image / jpg127.0.0.1:8000/api/v1 /

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

顺便说一下,我知道这一切都在curl的文档网站,但只是错过了所有这些东西一起的一个示例(很多选项试试)。

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

这篇关于使用json和djangorestframework将图像上传到ima​​gefield并使用CURL测试此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆