0

My IDE generates curl can export HTTP requests as curl commands. They look like this:

curl --request POST \
  --url http://test.api.local/api/tickets/route \
  --header 'accept-encoding: gzip' \
  --header 'content-type: application/json' \
  --data '{
  "start_datetime": "2020-11-10T09:30Z",
  "brand_id": 33,
  "model_id": 90,
  "class": 1,
  "country_code": "US",
  "contract_id": 120071,
}'

but when when I run them in Win10 command prompt or powershell there are a lot of errors:

PS C:\Users\Kosh> curl --request POST \
>>   --url http://test.api.local/api/tickets/route \
>>   --header 'accept-encoding: gzip' \
>>   --header 'content-type: application/json' \
>>   --data '{
>>   "start_datetime": "2020-11-10T09:30Z",
>>   "brand_id": 33,
>>   "model_id": 90,
>>   "class": 1,
>>   "country_code": "US",
>>   "contract_id": 120071,
>> }'
At line:2 char:5
+   --url http://entry.dev.tskad/api/tickets/route \
+     ~
Missing expression after unary operator '--'.
At line:2 char:5
+   --url http://entry.dev.tskad/api/tickets/route \
+     ~~~
Unexpected token 'url' in expression or statement.
At line:3 char:5
+   --header 'accept-encoding: gzip' \
+     ~
Missing expression after unary operator '--'.
At line:3 char:5
+   --header 'accept-encoding: gzip' \
+     ~~~~~~
Unexpected token 'header' in expression or statement.
At line:4 char:5
+   --header 'content-type: application/json' \
+     ~
Missing expression after unary operator '--'.
At line:4 char:5
+   --header 'content-type: application/json' \
+     ~~~~~~
Unexpected token 'header' in expression or statement.
At line:5 char:5
+   --data '{
+     ~
Missing expression after unary operator '--'.
At line:5 char:5
+   --data '{
+     ~~~~
Unexpected token 'data' in expression or statement.
At line:5 char:9
+   --data '{
+         ~
The Data section is missing its statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

Please tell me if there is a problem with line breaks and if yes how can I avoid it?

Andrei Kovrov
  • 1,186
  • 1
  • 9
  • 19
Kosh
  • 376
  • 2
  • 13

1 Answers1

1

You aren't looking for line breaks. What you really want is line continuation. The Line Continuation in PowerShell is the backtick character (`). It needs to be the last character in a line and follow a space character.

Likewise, the Windows command prompt also defines a line continuation character. In this case it is the ^ character. It also needs to be the final character and also needs to immediately follow a space character.

To solve your immediate issue replace all those trailing backslashes with backticks or ^ characters. You also need to add line continuation tokens to those lines that currently don't have any.

IInspectable
  • 35,521
  • 8
  • 69
  • 148