Issue
I am trying to query data from Azure App Insights API based on information here https://docs.microsoft.com/en-us/rest/api/application-insights/query/get
The request works just fine in Postman with different queries as well, but when I generate cUrl for the request in Postman and run it on a Linux machine it does not work. It returns no output nor error. When running it verbose I can see that I get 400 (Bad Request) error. At least I think so, here is the output from -v option:
* 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×pan=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
Here is the request generated by Postman:
curl --location --request GET 'https://api.applicationinsights.io/v1/apps/xxx/query?query=requests | take 10×pan=PT12H' --header 'x-api-key: xxx'
I tried to include all the headers from Postman (except the Auto Generated token which I find unlikely to be the cause) that are not included in the autogenerated cUrl if that might not be the cause. It did not work. Any insights on the issue are welcome
Solution
Try URLEncoding value of query like this requests %7C take 10
.
And your curl command would be:
curl --location --request GET 'https://api.applicationinsights.io/v1/apps/xxx/query?query=requests%20%7C%20take%2010×pan=PT12H' --header 'x-api-key: xxx'
Answered By - Sadiq Khoja Answer Checked By - Terry (WPSolving Volunteer)