Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows cURL cmd and multiline

Tags:

batch-file

cmd

I have installed cURL command line for windows.

Now I want to use this cURL for a REST api (redmine).

The most basic script I want to create is to push a new "past". But the problem is, one past can contains multiline text.

To put a new past I use:

curl -k -s -H "X-Redmine-API-Key: %API_KEY%" --data-urlencode "paste[text]=%TEXT%" %URL%

But the problem is when %TEXT% is composed by more than one line, the windows cmd failed.

Ex:

curl -k -s -H "X-Redmine-API-Key: XXXXXX" --data-urlencode "paste[text]=ST_METHOD_POST',    'POST',    true);
define('HTTP_REQUEST_METHOD_PUT',     'PUT',     true);
define('HTTP_REQUEST_METHOD_DELETE',  'DELETE',  true);" "https://etc"

And cmd stops after the first newline...

I try parse TEXT before running cmd by adding ^ at the end of the line to simulate multiline win cmd but does not work...


Update 1:

I try

curl -k -s -H "X-Redmine-API-Key: %API_KEY%" --data-urlencode "paste[text]@tmp.txt" %URL%

where tmp.txt equals to

hello world

but the request is transformed to

{"paste":{"id":xxx,"author_id":xxx,"project_id":xxx,"title":"Paste #xxx","text":" 
■h\u0000e\u0000l\u0000l\u0000o\u0000 \u0000w\u0000o\u0000r\u0000l\u0000d\u0000",
"created_on":"2012-11-26T09:24:15Z","updated_on":"2012-11-26T09:24:15Z"}}

And the final result does not work

like image 799
Kakawait Avatar asked Sep 19 '25 10:09

Kakawait


2 Answers

Here's what works perfectly to issue multi-line SSL POST curl request on Windows (it also works from batch/cmd file):

curl -i -k -X POST -H "Content-Type: text/xml" -d             ^
"^<?xml version=\"1.0\" encoding=\"UTF-8\" ?^>                ^
    ^<Transaction^>                                           ^
        ^<SomeParam1^>AD6084-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^>                                          ^
" "https://example.com/transaction"
like image 65
Gleb Esman Avatar answered Sep 23 '25 11:09

Gleb Esman


To successfully pass a linefeed to another command, you should use delayed expansion instead of perecent expansion.

Try it with

setlocal EnableDelayedExpansion
set LF=^


set "text=ST_METHOD_POST',    'POST',    true);"
set "text=!text!!LF!define('HTTP_REQUEST_METHOD_PUT',     'PUT',     true);"
set "text=!text!!LF!define('HTTP_REQUEST_METHOD_DELETE',  'DELETE',  true);"
echo ----
echo !text!
echo ----
curl -k -s -H "X-Redmine-API-Key: %API_KEY%" --data-urlencode "paste[text]=!TEXT!" !URL!

If text really contains linefeeds than this should work.

like image 35
jeb Avatar answered Sep 23 '25 10:09

jeb