【问题标题】:variables in curl api requestscurl api 请求中的变量
【发布时间】:2020-07-07 08:29:58
【问题描述】:

我一直在玩 api,我能够请求数据并解析 json 响应。 但是,我在请求中使用了变量,这些变量给了我问题。 如果变量不包含空格,那么一切都很好,但如果有空格,那么我会失败。

我就是这样做的

search="string with space"
curl -s 'https://apiurl.com' \
> -d 'search "'$search'";' \
> -H 'user-key: xxxxxxxxxxx' \
> -H 'Accept: application/json'
[
  {
    "title": "Syntax Error",
    "status": 400,
    "cause": "Mismatched input, double check your input. Common cause is sending \\\" instead of \"."
  }
]

谁能解释为什么会这样? 如果我在请求中使用带空格的字符串,而不使用变量,那么它没有问题。

【问题讨论】:

  • 试试-d 'search "'"$search"'";'
  • 谢谢。那行得通。

标签: bash api curl


【解决方案1】:

QUOTATION-MARK-19 病毒的经典案例。 :)

只需将'search "'$search'";' 替换为'search "'"$search"'";' 您所缺少的只是一个额外的双引号,以允许 $search 的变量扩展

search="string with space"
curl -s 'https://apiurl.com' \
> -d 'search "'"$search"'";' \
> -H 'user-key: xxxxxxxxxxx' \
> -H 'Accept: application/json'
[
  {
    "title": "Syntax Error",
    "status": 400,
    "cause": "Mismatched input, double check your input. Common cause is sending \\\" instead of \"."
  }
]

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多