windowscurl

cmd batch with curl to redirect http_code to file and content to another 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.com > "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.com **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 ?


Solution

  • 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.com > "http_code.tmp"