请求在邮递员中有效,但在 curl 中无效 - Azure App Insights API 以错误请求结束

Request works in postman but not in curl - Azure App Insights API ends with Bad Request

我正在尝试根据此处的信息 https://docs.microsoft.com/en-us/rest/api/application-insights/query/get

从 Azure App Insights API 查询数据

该请求在具有不同查询的 Postman 中工作得很好,但是当我在 Postman 中为请求生成 cUrl 并且 运行 它在 Linux 机器上时它不起作用。它 returns 没有输出也没有错误。当 运行 详细说明时,我可以看到我收到 400(错误请求)错误。至少我是这么认为的,这是 -v 选项的输出:

*   Trying 20.79.68.149...
* TCP_NODELAY set
* Connected to api.applicationinsights.io (20.79.68.149) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, [no content] (0):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS handshake, [no content] (0):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=WA; L=Redmond; O=Microsoft Corporation; CN=api.applicationinsights.io
*  start date: Jan 26 08:23:26 2022 GMT
*  expire date: Jan 21 08:23:26 2023 GMT
*  subjectAltName: host "api.applicationinsights.io" matched cert's "api.applicationinsights.io"
*  issuer: C=US; O=Microsoft Corporation; CN=Microsoft Azure TLS Issuing CA 01
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* TLSv1.3 (OUT), TLS app data, [no content] (0):
* Using Stream ID: 1 (easy handle 0x5607e18a9220)
* TLSv1.3 (OUT), TLS app data, [no content] (0):
> GET /v1/apps/xxxxxxxxxxxxxx/query?query=query=requests | take 10&timespan=PT12H HTTP/2
> Host: api.applicationinsights.io
> User-Agent: curl/7.61.1
> Accept: */*
> x-api-key: xxxxxxxxxxxxxx
>
 * TLSv1.3 (IN), TLS handshake, [no content] (0):
 * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
 * TLSv1.3 (IN), TLS handshake, [no content] (0):
 * TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
 * TLSv1.3 (IN), TLS app data, [no content] (0):
 * Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
 * TLSv1.3 (OUT), TLS app data, [no content] (0):
 * TLSv1.3 (IN), TLS app data, [no content] (0):
< HTTP/2 400
< date: Wed, 13 Apr 2022 18:34:46 GMT
< strict-transport-security: max-age=15724800; includeSubDomains
< access-control-allow-origin: *
<
 * Connection #0 to host api.applicationinsights.io left intact

这是 Postman 生成的请求:

curl --location --request GET 'https://api.applicationinsights.io/v1/apps/xxx/query?query=requests | take 10&timespan=PT12H' --header 'x-api-key: xxx'

我试图包括来自 Postman 的所有 headers(我认为不太可能是原因的自动生成令牌除外),如果这可能不是原因,则它们不包含在自动生成的 cUrl 中。那没起效。欢迎就此问题提出任何见解

像这样尝试查询的 URLEncoding 值 requests %7C take 10

你的 curl 命令是:

curl --location --request GET 'https://api.applicationinsights.io/v1/apps/xxx/query?query=requests%20%7C%20take%2010&timespan=PT12H' --header 'x-api-key: xxx'