最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - cmd batch with curl to redirect http_code to file and content to another file - Stack Overflow

programmeradmin6浏览0评论

I have a .cmd file with this content:

"curl.exe" -H "Content-Type: application/xml" -X POST --data @"post_content.tmp" -w "%{http_code}\n" -o "response_content.tmp" > "http_code.tmp"

I want to get the content of the response into a file and the http_code into another one.

When I run the .cmd I see this:

"curl.exe" -H "Content-Type: application/xml" -X POST --data @"post_content.tmp" -w "%{http_code}\n" -o "response_content.tmp" **1**> "http_code.tmp"

The 1 digit is added before the superior sign. And I have not the expected results.

But if I copy/paste the entire command line directly in the command window, it works.

How can I change my .cmd file ?

I have a .cmd file with this content:

"curl.exe" -H "Content-Type: application/xml" -X POST --data @"post_content.tmp" -w "%{http_code}\n" -o "response_content.tmp" https://myurl > "http_code.tmp"

I want to get the content of the response into a file and the http_code into another one.

When I run the .cmd I see this:

"curl.exe" -H "Content-Type: application/xml" -X POST --data @"post_content.tmp" -w "%{http_code}\n" -o "response_content.tmp" https://myurl **1**> "http_code.tmp"

The 1 digit is added before the superior sign. And I have not the expected results.

But if I copy/paste the entire command line directly in the command window, it works.

How can I change my .cmd file ?

Share Improve this question edited Mar 20 at 22:21 Robert 42.9k18 gold badges109 silver badges172 bronze badges asked Mar 20 at 16:02 coutier ericcoutier eric 1,0219 silver badges20 bronze badges 1
  • You have to escape all control characters. For example % -> %%. – Andry Commented Mar 20 at 19:23
Add a comment  | 

1 Answer 1

Reset to default 1

Commands entered directly on the Command Prompt CLI and commands in a .cmd script are processed differently.

On the CLI, % is treated as a literal, unless it is part of an environment variable like %path%. In a .cmd it is used to escape things like parameters %1 and control variables in loops %%i, so to use a literal %, you have to escape it by doubling up to %%.

If you don't want the command to print before executing, you can also add a @ to the start.

So this content for your .cmd would achieve the same:

@"curl.exe" -H "Content-Type: application/xml" -X POST --data @"post_content.tmp" -w "%%{http_code}\n" -o "response_content.tmp" https://myurl > "http_code.tmp"
发布评论

评论列表(0)

  1. 暂无评论