简体   繁体   English

如何将带有文件和参数的python3 post request转换为curl

[英]How to convert python3 post request with both files and params to curl

I have python3 script which correctly uploaded image to site and I want to do exactly the same via curl command. 我有python3脚本,可以将图像正确上传到站点,并且我想通过curl命令执行完全相同的操作。

import requests


url = 'http://1.2.3.4/upload'
files = {'image': open('foo.jpg', 'rb')}
params = {'bar': 'baz'}
requests.post(url, files=files, params=params)
# incorrect
curl -XPOST -F 'image=foo.jpg' -d 'bar=baz' 'http://1.2.3.4/upload'

I expect command like this will upload foo.jpg with param baz , but it seems to -F is ignored by curl when -d is specified. 我希望这样的命令将使用param baz上传foo.jpg ,但是当指定-d时, -F似乎会被curl忽略。

I want curl with file name specified, rather than whole file data inside command like curlify library does. 我想要指定文件名的curl ,而不是像curlify库那样在命令中包含整个文件数据。

  1. You can't use -F and -d together, former overrides the latter, 您不能同时使用-F-d ,前者会覆盖后者,
  2. You need to prefix filename with an @ sign, 您需要在文件名前面加上@符号,
  3. -F implies -X POST , and when it is used all data is sent as form-data, so bar=baz will not be appended to URL as with -G and -d , you need to append it manually. -F表示-X POST ,当使用它时,所有数据都作为表单数据发送,因此bar=baz不会像-G-d一样附加到URL上,您需要手动附加它。
curl -F 'image=@foo.jpg' 'http://1.2.3.4/upload?bar=baz'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM