【问题标题】:How to use variables inside data-raw json string in a curl command如何在 curl 命令中使用 data-raw json 字符串中的变量
【发布时间】:2022-01-09 11:00:29
【问题描述】:

以下 curl 命令脚本适用于双引号字符串中的变量,但是如何在 --data-raw '{...}' 中使用变量(例如 ip_addressuser_id)?

#!/bin/sh

ip_address="10.0.0.1"
user_id="my-user-id"
websec_token="some_string"
version="v12"

curl -w @curl-format.txt \
    --request POST "http://example.com/$version/web" \
    --header "Authorization: Bearer $websec_token" \
    --header "Content-Type: application/json" \
    --data-raw '{
        "ipAddress": "10.0.0.1",
        "userId": "my-user-id"
    }'

【问题讨论】:

    标签: json bash curl


    【解决方案1】:

    只需转义双引号:

    --data-raw "{
        \"ipAddress\": \"$ip_address\",
        \"userId\": \"$user_id\"
    }"
    

    【讨论】:

    • 谢谢@dan!有效。我尝试转义 \"{} 在单引号内。
    • @Rafiq 是的。在 shell 中,变量不会在单引号内展开。单引号按字面意思保留所有内容,但其他单引号字符除外。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    相关资源
    最近更新 更多