使用 curl 命令行发送/发布 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3007253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:09:55  来源:igfitidea点击:

send/post xml file using curl command line

xmlcommand-linecurl

提问by Arnab Sen Gupta

How can I send/post an xml file to a local server http://localhost:8080using curl from the command line?

如何从命令行使用 curl将 xml 文件发送/发布到本地服务器http://localhost:8080

What command should I use?

我应该使用什么命令?

回答by Peter Schuetze

If that question is connected to your other Hudson questions use the command they provide. This way with XML from the command line:

如果该问题与您的其他 Hudson 问题相关,请使用他们提供的命令。从命令行使用 XML 的这种方式:

$ curl -X POST -d '<run>...</run>' \
http://user:pass@myhost:myport/path/of/url

You need to change it a little bit to read from a file:

您需要稍微更改它以从文件中读取:

 $ curl -X POST -d @myfilename http://user:pass@myhost:myport/path/of/url

Read the manpage. following an abstract for -d Parameter.

阅读联机帮助页。遵循 -d 参数的摘要。

-d/--data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F/--form.

-d/--data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form field you may use --data-urlencode.

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'.

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar.

-d/--数据

(HTTP) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的那样。这将导致 curl 使用内容类型 application/x-www-form-urlencoded 将数据传递到服务器。与 -F/--form 比较。

-d/--data 与 --data-ascii 相同。要发布纯二进制数据,您应该改用 --data-binary 选项。要对表单字段的值进行 URL 编码,您可以使用 --data-urlencode。

如果在同一命令行上多次使用这些选项中的任何一个,则指定的数据片段将使用分隔符 & 符号合并在一起。因此,使用“-d name=daniel -d Skill=lousy”会生成一个看起来像“name=daniel&skill=lousy”的帖子块。

如果您以字母@ 开头数据,则其余部分应该是从中读取数据的文件名,或者 - 如果您希望 curl 从 stdin 读取数据。文件的内容必须已经是 URL 编码的。也可以指定多个文件。因此,从名为“foobar”的文件中发布数据将使用 --data @foobar 完成。

回答by Piskvor left the building

From the manpage, I believe these are the droids you are looking for:

联机帮助页,我相信这些是您正在寻找的机器人:

-F/--form <name=content>

(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign.

Example, to send your password file to the server, where 'password' is the name of the form-field to which /etc/passwd will be the input:

curl -F password=@/etc/passwd www.mypasswords.com

-F/--form <name=content>

(HTTP) 这让 curl 模拟用户按下提交按钮的填写表单。这会导致 curl 根据 RFC2388 使用 Content-Type multipart/form-data POST 数据。这允许上传二进制文件等。要强制“内容”部分成为文件,请在文件名前加上 @ 符号。

例如,将您的密码文件发送到服务器,其中 'password' 是 /etc/passwd 将作为输入的表单字段的名称:

curl -F password=@/etc/passwd www.mypasswords.com

So in your case, this would be something like
curl -F file=@/some/file/on/your/local/disk http://localhost:8080

所以在你的情况下,这将类似于
curl -F file=@/some/file/on/your/local/disk http://localhost:8080

回答by NgaNguyenDuy

You can using option --data with file.

您可以将选项 --data 与文件一起使用。

Write xml content to a file named is soap_get.xml and using curl command to send request:

将 xml 内容写入名为 soap_get.xml 的文件并使用 curl 命令发送请求:

curl -X POST --header "Content-Type:text/xml;charset=UTF-8" --data @soap_get.xml your_url

curl -X POST --header "Content-Type:text/xml;charset=UTF-8" --data @soap_get.xml your_url

回答by Steve HHH

With Jenkins 1.494, I was able to send a file to a job parameter on Ubuntu Linux 12.10 using curlwith --formparameters:

随着詹金斯1.494,我能够将文件发送到使用在Ubuntu Linux 12.10的作业参数curl--form参数:

curl --form name=myfileparam --form file=@/local/path/to/your/file.xml \
  -Fjson='{"parameter": {"name": "myfileparam", "file": "file"}}' \
  -Fsubmit=Build \
  http://user:password@jenkinsserver/job/jobname/build

On the Jenkins server, I configured a job that accepts a single parameter: a file upload parameter named myfileparam.

在 Jenkins 服务器上,我配置了一个接受单个参数的作业:一个名为myfileparam.

The first line of that curl call constructs a web form with a parameter named myfileparam(same as in the job); its value will be the contents of a file on the local file system named /local/path/to/your/file.txt. The @symbol prefix tells curl to send a local file instead of the given filename.

该 curl 调用的第一行构造了一个带有名为参数的 Web 表单myfileparam(与作业中的相同);它的值将是本地文件系统上名为/local/path/to/your/file.txt. 该@符号前缀告诉curl发送的,而不是给定文件名的本地文件。

The second line defines a JSON request that matches the form parameters on line one: a file parameter named myfileparam.

第二行定义了一个与第一行的表单参数匹配的 JSON 请求:一个名为myfileparam.

The third line activates the form's Build button. The forth line is the job URL with the "/build" suffix.

第三行激活窗体的构建按钮。第四行是带有“/build”后缀的作业 URL。

If this call is successful, curl returns 0. If it is unsuccessful, the error or exception from the service is printed to the console. This answer takes a lot from an old blog post relating to Hudson, which I deconstructed and re-worked for my own needs.

如果此调用成功, curl 返回0。如果不成功,来自服务的错误或异常将打印到控制台。这个答案从一篇与 Hudson 相关的旧博客文章中汲取了很多,我根据自己的需要对其进行了解构和重新设计。

回答by Gleb Esman

Here's how you can POST XML on Windows using curl command line on Windows. Better use batch/.cmd file for that:

以下是在 Windows 上使用 curl 命令行在 Windows 上发布 XML 的方法。最好使用 batch/.cmd 文件:

curl -i -X POST -H "Content-Type: text/xml" -d             ^
"^<?xml version=\"1.0\" encoding=\"UTF-8\" ?^>                ^
    ^<Transaction^>                                           ^
        ^<SomeParam1^>Some-Param-01^</SomeParam1^>            ^
        ^<Password^>SomePassW0rd^</Password^>                 ^
        ^<Transaction_Type^>00^</Transaction_Type^>           ^
        ^<CardHoldersName^>John Smith^</CardHoldersName^>     ^
        ^<DollarAmount^>9.97^</DollarAmount^>                 ^
        ^<Card_Number^>4111111111111111^</Card_Number^>       ^
        ^<Expiry_Date^>1118^</Expiry_Date^>                   ^
        ^<VerificationStr2^>123^</VerificationStr2^>          ^
        ^<CVD_Presence_Ind^>1^</CVD_Presence_Ind^>            ^
        ^<Reference_No^>Some Reference Text^</Reference_No^>  ^
        ^<Client_Email^>[email protected]^</Client_Email^>       ^
        ^<Client_IP^>123.4.56.7^</Client_IP^>                 ^
        ^<Tax1Amount^>^</Tax1Amount^>                         ^
        ^<Tax2Amount^>^</Tax2Amount^>                         ^
    ^</Transaction^>                                          ^
" "http://localhost:8080"

回答by Dheeraj R

If you have multiple headers then you might want to use the following:

如果您有多个标头,则可能需要使用以下内容:

curl -X POST --header "Content-Type:application/json" --header "X-Auth:AuthKey" --data @hello.json Your_url

回答by Musleh Uddin

You can use this command:

你可以使用这个命令:

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'Authorization: <<Removed>>' -F file=@"/home/xxx/Desktop/customers.json"  'API_SERVER_URL' -k 

回答by Paul Vargas

If you are using curlon Windows:

如果您在 Windows上使用curl

curl -H "Content-Type: application/xml" -d "<?xml version="""1.0""" encoding="""UTF-8""" standalone="""yes"""?><message><sender>Me</sender><content>Hello!</content></message>" http://localhost:8080/webapp/rest/hello