简体   繁体   English

如何在 curl 命令中使用数据原始 json 字符串中的变量

[英]How to use variables inside data-raw json string in a curl command

The following script of curl command works fine with variables inside double-quoted string, but how do I use variables (eg ip_address and user_id ) inside the --data-raw '{...}' ?以下 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"
    }'

Just escape the double quotes:只需转义双引号:

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

just add single quote arround the var part is better, as less \ introduced:只需在 var 部分周围添加单引号更好,因为 less \介绍:

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

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

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