用双引号卷曲 URL

Curl URL with double quote

我正在尝试将 CURL 请求中的 URL 字符串表示为:

curl -v --location --request GET "https://proxy-xxx.execute-api.ap-southeast-1.amazonaws.com/dev/proxy?path=\"https://target-xxx/api/v1/resource?q1=a&q2=b&q3=c\""

我会将 "https://target-xxx/api/v1/resource?q1=a&q2=b&q3=c\" 作为查询字符串传递给双引号中的路径变量。

在 API 网关控制台上测试 path="https://target-xxx/api/v1/resource?q1=a&q2=b&q3=c" 工作正常。

但是 CURL 遇到了错误的请求 400,而 aws api 网关没有收到。我相信 URL 语法问题。

URL 语法中的引号没有任何特殊意义,并不表示字符串文字。第一个 " 不标记未转义的字符串开始,第二个 " 不标记未转义的字符串结束。您应该 url 编码 & 个字符以避免拆分查询参数。

curl -v --location --request GET https://httpbin.org/get?path="https://target-xxx/api/v1/resource?q1=a%26q2=b%26q3=c"

回复:

{
  "args": {
    "path": "https://target-xxx/api/v1/resource?q1=a&q2=b&q3=c"
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.79.1",
    "X-Amzn-Trace-Id": "Root=1-6215a80a-5e94b80531a2f4076f84342e"
  },
  "url": "https://httpbin.org/get?path=https:%2F%2Ftarget-xxx%2Fapi%2Fv1%2Fresource%3Fq1=a%26q2=b%26q3=c"
}

如果你希望path引用

curl -v --location --request GET https://httpbin.org/get?path=\"https://target-xxx/api/v1/resource?q1=a%26q2=b%26q3=c\"

回复:

{
  "args": {
    "path": "path": "\"https://target-xxx/api/v1/resource?q1=a&q2=b&q3=c\""
  },
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.79.1",
    "X-Amzn-Trace-Id": "Root=1-6215a9c5-11a283486ad54eb96c499bc7"
  },
  "url": "https://httpbin.org/get?path=\"https:%2F%2Ftarget-xxx%2Fapi%2Fv1%2Fresource%3Fq1=a%26q2=b%26q3=c\""
}