简体   繁体   English

使用 cURL 命令行 POST XML 文件

[英]POST XML file using cURL command line

How can I POST an XML file to a local server http://localhost:8080 using cURL from the command line?如何从命令行使用 cURL 将 XML 文件发布到本地服务器http://localhost:8080

What command should I use?我应该使用什么命令?

If that question is connected to your other Hudson questions use the command they provide.如果该问题与您的其他 Hudson 问题相关,请使用他们提供的命令。 This way with XML from the command line:从命令行使用 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 -d/--数据

(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. (HTTP) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的那样。 This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.这将导致 curl 使用内容类型 application/x-www-form-urlencoded 将数据传递到服务器。 Compare to -F/--form.与 -F/--form 比较。

-d/--data is the same as --data-ascii. -d/--data 与 --data-ascii 相同。 To post data purely binary, you should instead use the --data-binary option.要发布纯二进制数据,您应该改用 --data-binary 选项。 To URL-encode the value of a form field you may use --data-urlencode.要对表单字段的值进行 URL 编码,您可以使用 --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'.因此,使用“-d name=daniel -d Skill=lousy”会生成一个看起来像“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.如果您以字母@ 开头数据,则其余部分应该是从中读取数据的文件名,或者 - 如果您希望 curl 从 stdin 读取数据。 The contents of the file must already be URL-encoded.文件的内容必须已经是 URL 编码的。 Multiple files can also be specified.也可以指定多个文件。 Posting data from a file named 'foobar' would thus be done with --data @foobar.因此,从名为“foobar”的文件中发布数据将使用 --data @foobar 完成。

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. (HTTP) 这让 curl 模拟用户按下提交按钮的填写表单。 This causes curl to POST data using the Content-Type multipart/form-data according to RFC2388.这会导致 curl 根据 RFC2388 使用 Content-Type multipart/form-data POST 数据。 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:例如,将您的密码文件发送到服务器,其中 '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

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

With Jenkins 1.494, I was able to send a file to a job parameter on Ubuntu Linux 12.10 using curl with --form parameters:使用 Jenkins 1.494,我能够使用curl--form参数将文件发送到 Ubuntu Linux 12.10 上的作业参数:

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);该 curl 调用的第一行使用名为myfileparam的参数构造了一个 Web 表单(与作业中的相同); its value will be the contents of a file on the local file system named /local/path/to/your/file.txt .它的值将是本地文件系统上名为/local/path/to/your/file.txt的文件的内容。 The @ symbol prefix tells curl to send a local file instead of the given filename. @符号前缀告诉 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 .如果此调用成功, curl 返回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.这个答案从一篇与 Hudson 相关的旧博客文章中汲取了很多,我根据自己的需要对其进行了解构和重新设计。

Here's how you can POST XML on Windows using curl command line on Windows.以下是在 Windows 上使用 curl 命令行在 Windows 上发布 XML 的方法。 Better use batch/.cmd file for that:最好使用 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^>john@smith.com^</Client_Email^>       ^
        ^<Client_IP^>123.4.56.7^</Client_IP^>                 ^
        ^<Tax1Amount^>^</Tax1Amount^>                         ^
        ^<Tax2Amount^>^</Tax2Amount^>                         ^
    ^</Transaction^>                                          ^
" "http://localhost:8080"

你可以使用这个命令:

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 

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

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

如果您在 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

Powershell + Curl + Zimbra SOAP API Powershell + Curl + Zimbra SOAP API

${my_xml} = @"
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
  <soapenv:Body>
   <GetFolderRequest xmlns=\"urn:zimbraMail\">
    <folder>
       <path>Folder Name</path>
    </folder>
   </GetFolderRequest>
  </soapenv:Body>
</soapenv:Envelope>
"@

${my_curl} = "c:\curl.exe"
${cookie} = "c:\cookie.txt"

${zimbra_soap_url} = "https://zimbra:7071/service/admin/soap"
${curl_getfolder_args} = "-b", "${cookie}",
            "--header", "Content-Type: text/xml;charset=UTF-8",
            "--silent",
            "--data-raw", "${my_xml}",
            "--url", "${zimbra_soap_url}"

[xml]${my_response} = & ${my_curl} ${curl_getfolder_args}
${my_response}.Envelope.Body.GetFolderResponse.folder.id

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

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