简体   繁体   English

Bash 脚本 - 带有变量的 cURL

[英]Bash script - cURL with variables

I'm trying to get a cURL script to input my variables in a --data section.我正在尝试获取一个 cURL 脚本以在 --data 部分中输入我的变量。 I'm fairly new to this, but it's just inserting the variables names.我对此很陌生,但它只是插入变量名称。

The background is this script is called to hit an API in our ticketing system to create a new job.背景是调用此脚本以命中我们的票务系统中的 API 以创建新作业。 I am finding the ticket that is created has the subject "${DESCRIPTION}" and not "problem description".我发现创建的票的主题是“${DESCRIPTION}”而不是“问题描述”。

#!/bin/bash
# This will log a ticket in Ticketing System


DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'

curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
        "helpdesk_ticket":
        {
                "description": "${DESCRIPTION}",
                "subject": "${SUBJECT}",
                "email": "email@domain.com",
                "priority": 1,
                "status": 2

        },
                "cc_emails": ""

        }'

As per always, got this working just after I posted....和往常一样,在我发布后就开始工作了......

The solution was to wrap the variable in "'".解决方案是将变量包装在“'”中。

#!/bin/bash
# This will log a ticket in Ticketing System


DESCRIPTION='Problem Description'
SUBJECT='Problem Subject'

curl --location --request POST 'https://XXXXX.domain.com/helpdesk/tickets.json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data '{
         "helpdesk_ticket":
    {
            "description": "'"$DESCRIPTION"'",
            "subject": "'"$SUBJECT"'",
                "email": "email@domain.com",
                "priority": 1,
                "status": 2

        },
                "cc_emails": ""

        }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM