POST multiple files with -d in curl

情到浓时终转凉″ 提交于 2020-06-29 04:58:08

问题


I'm using curl to create several classifications. I have written the json for the many classifications and they are in one folder. I would like to create all the classifications in one go. But using curl I can only create them one at a time. How could I make them in one request?

curl -u admin:admin -H "Content-Type: application/json" -X POST -d @pii.json  http://127.0.0.1:21000/api/atlas/v2/types/typedefs

The curl manual for -d says 'Multiple files can also be specified'. How can I do this? All my attempts have failed.

Do I need a bash script instead? If so, could you help me - I'm not a coder and I'm struggling without an example!

Thanks in advance.


回答1:


I found the following to work in the end:

<fileToUpload.dat xargs -I %  curl -X POST -T "{%}" -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs

Where fileToUpload.dat contained a list of the .json files.

This seemed to work over Daniel's answer, probably due to the contents of the files. Hopefully this is useful to others if Daniel's solution doesn't work for them.




回答2:


You probably don't want to use multiple -d with JSON data since curl concatenates multiple ones with a & in between. As described in the man page for -d/--data:

If any of these options is used more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would generate a post chunk that looks like 'name=daniel&skill=lousy'.

You can however easily and conveniently pass several files on stdin to let curl use them all in one go:

cat a.json b.json c.json | curl -d@- -u admin:admin -H "Content-Type: application/json" http://127.0.0.1:21000/api/atlas/v2/types/typedefs

(please note that -X POST has no place on a command line that uses -d)



来源:https://stackoverflow.com/questions/49101319/post-multiple-files-with-d-in-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!