Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape cURL command in Yaml which contains quotes and apostrophes

I've got this cURL command:

curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX

I need to use this in a GitLab CI file which has the Yaml support. The Yaml parser does not accept it. Normally I would surround it in quotes but I already use both quotes and apostrophes in the command.

This is how the command looks right now.

How can I escape the sequence properly?

deploy:
  stage: deploy
  script:
    - "curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX"
like image 209
Hedge Avatar asked Apr 05 '17 06:04

Hedge


People also ask

How do you escape spaces in Yaml?

In YAML, \n is used to represent a new line, \t is used to represent a tab, and \\ is used to represent the slash. In addition, since whitespace is folded, YAML uses bash style "\ " to escape additional spaces that are part of the content and should not be folded.

How do you escape in curl?

The backslash ( \ ) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

What is curl Dquote?

The DQUOTE function extracts characters enclosed in double quote marks.


1 Answers

The easiest way would be to use a block scalar:

deploy:
  stage: deploy
  script: |-
    curl -X POST --data-urlencode 'payload={"text": "A new version has been deployed to production."}' https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX

| starts a literal block scalar, - tells YAML to discard the trailing newline (which would otherwise be part of the scalar). For readability, you can use a folded block scalar instead (newlines will be converted to spaces):

deploy:
  stage: deploy
  script: >-
    curl -X POST --data-urlencode 'payload={"text":
    "A new version has been deployed to production."}'
    https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX

Finally, it is also possible to use double quotes, as long as you escape the double quotes inside the scalar (newlines are also folded into spaces):

deploy:
  stage: deploy
  script: 
    "curl -X POST --data-urlencode 'payload={\"text\":
     \"A new version has been deployed to production.\"}' 
     https://hooks.slack.com/services/XXXXXXX/XXXXXXXXX/XXXXXXXXXXXXX"
like image 151
flyx Avatar answered Sep 22 '22 17:09

flyx