bash - 如何在 Bash 脚本中将变量传递给 Curl
问题描述
如何在 Bash 脚本中将变量传递给 Curl
我无法让这个 curl 使用环境变量在 bash 脚本中工作。API 密钥在通过变量传入时会以某种方式被误解,并且我在提交时遇到身份验证错误。如果我将没有引号的 API 密钥作为纯文本插入,则效果很好。我尝试了各种形式的转义引号和其他组合。任何帮助都会很棒。
代码
#!/bin/bash
source .env
curl -X POST https://api.easypost.com/v2/shipments \
-u "$EASYPOST_TEST_API_KEY": \
-d 'shipment[to_address][name]=Dr. Steve Brule' \
-d 'shipment[to_address][street1]=179 N Harbor Dr' \
-d 'shipment[to_address][city]=Redondo Beach' \
-d 'shipment[to_address][state]=CA' \
-d 'shipment[to_address][zip]=90277' \
-d 'shipment[to_address][country]=US' \
-d 'shipment[to_address][phone]=8573875756' \
-d 'shipment[to_address][email]=dr_steve_brule@gmail.com' \
-d 'shipment[from_address][name]=EasyPost' \
-d 'shipment[from_address][street1]=417 Montgomery Street' \
-d 'shipment[from_address][street2]=5th Floor' \
-d 'shipment[from_address][city]=San Francisco' \
-d 'shipment[from_address][state]=CA' \
-d 'shipment[from_address][zip]=94104' \
-d 'shipment[from_address][country]=US' \
-d 'shipment[from_address][phone]=4153334445' \
-d 'shipment[from_address][email]=support@easypost.com' \
-d 'shipment[parcel][length]=20.2' \
-d 'shipment[parcel][width]=10.9' \
-d 'shipment[parcel][height]=5' \
-d 'shipment[parcel][weight]=65.9' \
解决方案
根据 Gordon Davisson 将其分解为最小请求的建议,它开始正确发送。然后我重建了它,就像我上面的问题一样,无论出于何种原因,它都像一个魅力。该-x
标志在解决此问题方面也非常有帮助,但首先发现它本身并没有任何问题。感谢大家的回复!
推荐阅读
- postgresql - pgjdbc中的loginTimeout、connectTimeout和socketTimeout有什么区别
- azure - 在 Azure AD 中,如何通过 PowerShell(如 Set-AzureADUser 和 Get-AzureADUser)设置和读取用户的主邮箱?
- javascript - 输出奇怪的主任务和微任务的执行顺序
- azure-web-app-service - 在一个应用服务计划下托管多个网站的 Azure 部署槽
- javascript - 为什么我的图像滑块上的 background-size 或 background-repeat 属性不起作用?
- javascript - 我有一个 JS 函数,它应该根据 DropDownListFor 中的事件更改来更改 td 的 innerHTML,但仅适用于初始加载
- excel - 汇总时间记录以准确反映小时:分钟:秒
- r - r rvest 错误:“doc_namespaces(doc) 中的错误:外部指针无效”
- sql - 如何在 SQL Server 中的两个日期之间进行选择
- android - 使用 diff utils 更新回收器视图适配器,添加项目时无法正常工作