Johnny Yip
Johnny Yip

Reputation: 99

How to run curl request in yml

I have the following curl request which I want to use in my config.yml file for my CircleCI pipeline.

But it keeps giving me errors and I cannot find the right syntax to get it to work. I put the file in YAML lint and the error is mapping values are not allowed in this context. Here is my curl request

curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a test!"}' [WEBHOOK URL]

Here is my config.yml file:

version: 2.1

jobs:
  build:
    working_directory: ~/circleci-python
    docker:
      - image: "circleci/python:3.6.4"
    steps:
      - checkout
      - run: python3 main.py
  test:
    working_directory: ~/circleci-python
    docker:
      - image: "circleci/python:3.6.4"
    steps:
      - checkout
      - run: python3 main-test.py      
      - run: curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a test!"}' [WEBHOOK URL]
workflows:
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build

Upvotes: 4

Views: 14816

Answers (1)

flyx
flyx

Reputation: 39708

Use a folded block scalar:

version: 2.1

jobs:
  build:
    working_directory: ~/circleci-python
    docker:
      - image: "circleci/python:3.6.4"
    steps:
      - checkout
      - run: python3 main.py
  test:
    working_directory: ~/circleci-python
    docker:
      - image: "circleci/python:3.6.4"
    steps:
      - checkout
      - run: python3 main-test.py      
      - run: >-
          curl -X POST -H 'Content-type: application/json'
          --data '{"text":"This is a test!"}' [WEBHOOK URL]
workflows:
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build

> starts a folded block scalar. The following - tells YAML to strip the final line break which would otherwise be part of the value.

Block scalars do not use quotes or escape sequences and end when their indentation level is left. They are ideal for entering values that use syntax similar to YAML. Folded block scalars fold line breaks into a single space, which allows you to break the command into multiple lines, as I did here.

The reason you get an error is that YAML reads curl -X POST -H 'Content-type as an implicit key – think of it like b in the line - a: b: c. This is not allowed by YAML syntax. The single quote in the curl line does not prevent this since quotation that is inside an unquoted scalar is just parsed as content without any special rules.

Upvotes: 6

Related Questions