gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:40:09 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com