how to use cURL with a variable in the URL string All In One
how to use cURL with a variable in the URL string All In One
如何在
cURL
的URL
字符串中使用变量
系统变量
环境变量
shell 变量
# cURL 字符串中如何使用 shell 系统环境变量 ❓
$ export DD_ROBOT_TOKEN=404e99******36d17fa1202
$ echo $DD_ROBOT_TOKEN
# 404e99******36d17fa1202
# "content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n $(ifconfig | grep "192.168")"
errors
$ curl 'https://oapi.dingtalk.com/robot/send?access_token=${DD_ROBOT_TOKEN}'' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n ???"
}
}'
# {"errcode":300001,"errmsg":"错误描述: robot 不存在;解决方案:请确认 token 是否正确;"} ❌
solutions
$ curl "https://oapi.dingtalk.com/robot/send?access_token=${DD_ROBOT_TOKEN}" \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n ???"
}
}'
# {"errcode":0,"errmsg":"ok"} ✅
# cURL use variable inside "double quotes" ✅
# content use 'single quotes wrap "double quotes"' ✅
$ curl "https://oapi.dingtalk.com/robot/send?access_token=${DD_ROBOT_TOKEN}" \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n '"${DD_ROBOT_TOKEN}"'"
}
}'
# \" escape double quote ✅
$ curl "https://oapi.dingtalk.com/robot/send?access_token=${DD_ROBOT_TOKEN}" \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n \"${DD_ROBOT_TOKEN}\""
}
}'
escape bug ❌
# \" escape all double quote inside double quote ✅
$ curl "https://oapi.dingtalk.com/robot/send?access_token=${DD_ROBOT_TOKEN}" \
-H "Content-Type: application/json" \
-d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n ${DD_ROBOT_TOKEN}\"
}
}"
$DD_ROBOT_TOKEN
vs ${DD_ROBOT_TOKEN}
# ${DD_ROBOT_TOKEN} ✅
# $DD_ROBOT_TOKEN ✅
$ curl "https://oapi.dingtalk.com/robot/send?access_token=$DD_ROBOT_TOKEN" \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "text",
"text": {
"content": "测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n '"$DD_ROBOT_TOKEN"'"
}
}'
#
$ curl "https://oapi.dingtalk.com/robot/send?access_token=$DD_ROBOT_TOKEN" \
-H "Content-Type: application/json" \
-d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n $DD_ROBOT_TOKEN\"
}
}"
demos
- Raspberry Pi 开机自启动脚本,自动发送树莓派的 IP 地址 (钉钉 webhook)
#!/usr/bin/env bash
# coding: utf8
# 自动发送树莓派 ip 地址,到钉钉上
# cURL 字符串中使用系统变量
# $ export DD_ROBOT_TOKEN=404e996c8747ea4a******3f39ce36d17fa1202
DD_ROBOT_TOKEN=404e996c8747ea4a******3f39ce36d17fa1202
echo $DD_ROBOT_TOKEN
# ip4=$(/sbin/ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1)
# ip6=$(/sbin/ip -o -6 addr list eth0 | awk '{print $4}' | cut -d/ -f1)
# echo $ip4
# echo $ip6
# ❌
# RPI_IP=ifconfig | grep "192.168"
# ✅ $(可执行命令)
# RPI_IP=$(ifconfig | grep "192.168")
# ❌
# ifconfig | grep "192.168" > ip.md
# RPI_IP=echo ./ip.md
# ✅ $(可执行命令)
$(ifconfig | grep "192.168" > ip.md)
# ❌ echo file, ??? echo 需要读取输出流
# RPI_IP=$(echo ./ip.md)
# ✅ read file
RPI_IP=$(cat ./ip.md)
echo $RPI_IP
## 高级命令 sed
# 测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n $RPI_IP
# 测试:钉钉群机器人 / 自定义消息机器人🤖\n 树莓派开机自动发送 IP 地址消息:\n ${RPI_IP}
MARKDOWN='
{
"msgtype": "markdown",
"markdown": {
"title":"🤖 树莓派开机自动发送 Wi-Fi IP 地址消息: ",
"text": "🍓 Raspberry Pi IP: '${RPI_IP}'\n🔑 API Token: '$DD_ROBOT_TOKEN'\n"
},
"at": {
"atUserIds": [
"xgqfrms"
],
"isAtAll": false
}
}'
# echo $MARKDOWN
curl "https://oapi.dingtalk.com/robot/send?access_token=$DD_ROBOT_TOKEN" \
-H "Content-Type: application/json" \
-d "$MARKDOWN"
echo "finished ✅"
- 读书会打卡机器人(企业微信 webhook)
crontab
定时器/定时任务
#!/bin/sh
# 设置 set
set -eux
# 设置 env
LANGUAGE="zh-CN"
UA="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4343.0 Safari/537.36"
SH_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T');
# 打印参数
echo "✅ SH_DATE = ${SH_DATE}"
# github env {REPORT_DATE, WEBHOOK_KEY}
echo "✅REPORT_DATE = ${REPORT_DATE}"
echo "✅WEBHOOK_KEY = ${WEBHOOK_KEY}"
# 读书会机器人🤖️ features:
# 1. 大大简化进入打卡的流程,跳过 3s 首页的推荐;
# 2. 每天早晚两次打卡提醒(08:00 和 20:00) ⏰,降低忘记打卡的风险;
MARKDOWN='
{
"msgtype": "markdown",
"markdown": {
"content": "⏰ '$SH_DATE'\n\n🎉<font color=\"warning\">读书会快捷链接</font>\n\n📘[<font color=\"info\">阅读打卡链接</font>](https://library.xgqfrms.xyz/#/club/share)\n\n👨🏻💻[<font color=\"info\">我的链接</font>](https://library.xgqfrms.xyz/#/my)\n\n💰[<font color=\"info\">积分链接</font>](https://library.xgqfrms.xyz/#/mall)\n\n<font color=\"warning\">读书会机器人🤖️ features:</font>\n\n<font color=\"comment\">1. 大大简化进入打卡的流程,跳过 3s 首页的推荐;</font>\n\n<font color=\"comment\">2. 每天早晚两次打卡提醒(08:00 和 20:00) ⏰,降低忘记打卡的风险;</font>\n\n",
"mentioned_list":["凌晨"],
}
}'
# 只有双引号中才可以包含变量,单引号不可以包含变量
curl "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${WEBHOOK_KEY}" \
-H 'Content-Type: application/json' \
-H "Accept-Language: $LANGUAGE" \
-H "User-Agent: $UA" \
-d "$MARKDOWN"
https://github.com/xgqfrms/wx-robot-notice/blob/main/src/robot.sh#L64
shell env
# 绝对路径
#!/bin/sh
#!/usr/bin/env sh
# 相对路径
#!/usr/bin/env shell
#!/usr/bin/env bash
#!/usr/bin/env zsh
There are many solutions, here are some comparisons
If you want to use variable
in the cURL's string parameters, you should use double quotes
to enclose the variables.
By the way, curl's option --data-raw
alias is -d
If you want to use variable
in the cURL's string parameters, you should use double quotes
to enclose the variables.
--data-raw
alias is -d
1. just wrap your data as a normal variable (🚀 awesome)
use variable method 1:
${ip_address}
✅
# define data variable
raw_data='{
"ipAddress": "'$ip_address'",
"userId": "'$user_id'"
}'
# use data variable ✅
-d "$raw_data"
use variable method 2:
${ip_address}
✅
# define data variable
raw_data='{
"ipAddress": "'${ip_address}'",
"userId": "'${user_id}'"
}'
# use data variable ✅
-d "$raw_data"
2. use ''
single quotes in ""
double quotes (👍 good)
# nested "''"
-d '{
"ipAddress": "'$ip_address'",
"userId": "'$user_id'"
}'
3. use \
escape all "
in a double quotes (👎 not good)
# escape \"
-d "{
\"ipAddress\": \"$ip_address\",
\"userId\": \"$user_id\"
}"
refs
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
2>&1
错误,二合一输出
#!/usr/bin/env bash
# coding: utf8
# ip-program.sh
/usr/bin/bash -u /home/pi/Desktop/dd-ip-notice-robot.sh > /home/pi/Desktop/ip-program.log 2>&1
https://www.cnblogs.com/xgqfrms/tag/2>%261/
refs
https://www.cnblogs.com/xgqfrms/tag/cURL/
https://www.cnblogs.com/xgqfrms/tag/curl to fetch/
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17322420.html
未经授权禁止转载,违者必究!