curl - 如何在 yml 中运行 curl 请求

标签 curl yaml

我有以下 curl 请求,我想在我的 CircleCI 管道的 config.yml 文件中使用它。

但它一直给我错误,我找不到正确的语法来让它工作。 我将文件放在 YAML lint 中,错误是在此上下文中不允许映射值。 这是我的 curl 请求

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

这是我的 config.yml 文件:

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

最佳答案

使用折叠 block 标量:

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

> 开始折叠 block 标量。以下 - 告诉 YAML 去除最后的换行符,否则它会成为值的一部分。

block 标量不使用引号或转义序列,并在离开缩进级别时结束。它们非常适合输入使用类似于 YAML 的语法的值。折叠 block 标量将行折叠成一个空格,这样您就可以将命令分成多行,就像我在这里所做的那样。

出现错误的原因是 YAML 将 curl -X POST -H 'Content-type 读取为隐式键 - 将其视为行中的 b -a:b:c。 YAML 语法不允许这样做。 curl 行中的单引号不会阻止这种情况,因为未加引号的标量内的引号仅被解析为内容,没有任何特殊规则。

关于curl - 如何在 yml 中运行 curl 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68268869/

相关文章:

ruby - 将对象序列化为 JSON、XML、YAML?

php - cURL 上传到 Google 云端硬盘

r - 在 RCurl 中创建 C 级文件句柄,用于写入下载的文件

groovy加载YAML文件修改并将其写入文件中

amazon-web-services - 如何在 aws cloudformation 模板中限制特定于 SQS 的 IAM 角色

yaml - AWS SAM lambda 目标

regex - 使用ansible从变量中解析子字符串

curl - cygwin- curl : (3) Port number ended with '"' error

php - 重用来自 cURL 多处理程序的句柄

php - 由于数据未插入数据库,无法回显网站