Curl: one-liner to test http/2 support - unit-testing

I'm currently writing an unit test to check if http/2 is supported.
Is there a curl one-liner which checks if http/2 is supported and outputs a response that is easy to parse?

HTTP/2 supported:
$ curl -sI https://curl.se -o/dev/null -w '%{http_version}\n'
2
HTTP/2 not supported (instead serving 1.1 in this case):
$ curl -sI http://curl.se -o/dev/null -w '%{http_version}\n'
1.1
(curl 7.50.0 or later is required for this command line to work)

Run
curl --version
and look for HTTP2 on the Features list

Here you can find a list of Tools for debugging, testing and using HTTP/2.
Probably the easiest one from the command line is:
$ is-http2 www.cloudflare.com
But that requires npm install -g is-http2-cli
For testing using curl you may need to compile it using the nghttp library, in macOS this can be done by using brew you could use:
$ brew install curl --with-nghttp2
And then you could use what #daniel-stenberg suggests in his answer
$ curl -sI https://curl.haxx.se -o/dev/null -w '%{http_version}\n'
In where you will get a 2 if http2 is supported.

I used
curl -kvso /dev/null --http2 https://app.domain.com:443
which returned
...
> GET / HTTP/2
...
< HTTP/2 302
...
this is not checking if HTTP2 is supported, but check if HTTP2 actually works.

You could simply do:
curl --http2 --head domain.com

Accepted answer doesn't work for h2c in the example I'm looking at. Instead use:
curl --http2 -sI https://curl.se -o/dev/null -w '%{http_version}\n'

Related

wso2 mutual ssl curl command APIM 3.2.0

I tried setup ALLINONE in my local, followed the documentation https://apim.docs.wso2.com/en/latest/learn/api-security/api-authentication/secure-apis-using-mutual-ssl/
it worked, but what will be the curl command for request because the document talks about testing only through postman
You can use the following curl commands when you want to use the header-based approach.
curl -X GET -H "X-WSO2-CLIENT-CERTIFICATE: (Base64 encoded public cert)" "https://localhost:8243/mock/v1" -v
curl -X GET -H "X-WSO2-CLIENT-CERTIFICATE: (Base64 encoded public cert)" "http://localhost:8280/mock/v1" -v
In order to work this, you need to add the following configuration to the deployment.toml in wso2am-3.2.0/repository/conf location.
[apimgt.mutual_ssl]
enable_client_validation = false
You can use the following curl commands if you are using the cert and key.
curl -k --cert int.ext.wso2.com.crt --key int.ext.wso2.com.key -X GET "https://localhost:8243/mock/v1" -v

How-to perform a simple AWS/EC2 rest API call with Curl

Has anyone used cUrl to manage the AWS (EC2) rest API? Here I picked the simplest possible call, listing running instances. Am I missing something or am I clearly showing ignorance of proper cUrl usage? The only real lead I have is this post from 2014 aside from the scattered AWS docs which don't show an actual complete example. (I do realize there are SDKs). It's a curl expert I need...I guess and someone to help me get past the over-use of the word "canonical" by the AWS tech writer.
Rest API
DescribeImages Action
Create Canonical Request
Bash:
amz_access_key_ID="????"
amz_ec2_secret="????"
amz_host=ec2.amazonaws.com
amz_date8=`date -u "+%Y%m%d"`
amz_date_http=`date -uR`
amz_date_rfc8601=`date -u "+%Y%m%dT%H%M%SZ"`
amz_content_type="application/json"
amz_credential="${amz_access_key_ID}/${amz_date8}/us-west-2/ec2/aws4_request"
amz_signed_headers="${amz_host};${amz_date_rfc8601};${amz_content_type}"
amz_signature=`echo -en ${amz_signed_headers} | openssl sha256 -hmac ${amz_ec2_secret} -binary | base64`
params="Action:DescribeInstances;Version:2016-11-15;X-Amz-Algorithm:AWS4-HMAC-SHA256;X-Amz-Credential:${amz_credential};X-Amz-Date:${amz_date_rfc8601};X-Amz-SignedHeaders:${amz_signed_headers};X-Amz-Signature:${amz_signature}"
curl -X POST -H "Content-Type:${amz_content_type}" -H "Date:${amz_date_http}" -H "Host:${amz_host}" -F "${params}" http://ec2.amazonaws.com/
Result from Amazon:
<!doctype html><html...HTTP Status 500 – Internal Server Error</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Encountered an Internal Error</p><p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p><hr class="line" /></body>
The AWS API requests are unfortunately not straight forward to implement in curl. You are at least missing the canonical request that looks like that (from the example from the docs):
GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
I recently started a litte project at https://github.com/sengaya/aws-micro-cli that implements some basic API calls of s3 and sts. While it does not support ec2 at this point you can look at the code or run something like this to get the final curl output as well as the steps to craft the request:
AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar aws-micro s3 ls
Full debug output:
s3://some-bucket --debug --dryrun
DEBUG - get_bucket_from_s3url: some-bucket
DEBUG - get_key_from_s3url:
DEBUG - create_request_url: https://some-bucket.s3.amazonaws.com/
DEBUG - get_host_from_request_url: some-bucket.s3.amazonaws.com
DEBUG - array_sort: host:some-bucket.s3.amazonaws.com
x-amz-content-sha256:123456789123456789123456789
x-amz-date:20201007T155300Z
DEBUG - array_sort: host
x-amz-content-sha256
x-amz-date
DEBUG - create_canonical_and_signed_headers: host:some-bucket.s3.amazonaws.com
x-amz-content-sha256:123456789123456789123456789
x-amz-date:20201007T155300Z
host;x-amz-content-sha256;x-amz-date
DEBUG - get_canonical_uri: /
DEBUG - create_canonical_request: GET
/
host:some-bucket.s3.amazonaws.com
x-amz-content-sha256:123456789123456789123456789
x-amz-date:20201007T155300Z
host;x-amz-content-sha256;x-amz-date
123456789123456789123456789
DEBUG - sha256: 123456789123456789123456789
DEBUG - create_string_to_sign: AWS4-HMAC-SHA256
20201007T155300Z
20201007//s3/aws4_request
123456789123456789123456789
DEBUG - create_authorization_header: AWS4-HMAC-SHA256 Credential=foo/20201007//s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=123456789123456789123456789
curl -v --fail https://some-bucket.s3.amazonaws.com/ -H Authorization: AWS4-HMAC-SHA256 Credential=foo/20201007//s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=123456789123456789123456789 -H x-amz-content-sha256:123456789123456789123456789 -H x-amz-date:20201007T155300Z

How to set the --compressed curl command option using curl_easy?

When I run curl --help I see the following option available:
--compressed Request compressed response (using deflate or gzip)
However, when I visit the documentation on curl_easy's setopt command, I don't see an option to pass in this compressed flag.
How can I set --compressed using curl_easy_setopt please?
--compressed is equivalent to CURLOPT_ACCEPT_ENCODING.

tshark - filtering by webservice's name

I'm trying to use tshark to record each request sent to a WebService called myservice.
When I use below command, I can see in output file every request sent on port 8280 (Tomcat) :
tshark -n -i eth1 port 8280 -w output.pcap
Considering I have a lot of WebServices in that Tomcat instance, I would like to filter by service name, something like that :
tshark -n -i eth1 port 8280 -w output.pcap -R 'http.request.uri contains "myservice"'
According to man, it looks like I should rather use -f (capture filter) than -R (display filter) since you can't use -R with -w :
tshark: Read filters aren't supported when capturing and saving the captured packets.
I took a look at documentation about capture filters but I can't see a way to do that. I also tried with tcpdump without success.
You can use ngrep for this
sudo ngrep -O output.pcap -i -d eth0 'myservice'
Although you might get some false positive depending on whether or not 'myservice' is found on an irrelevant packet that was not intended for your application. To avoid this, you might want to apply a bpf filter to grep only traffic that was directed to your service/app
sudo ngrep -O output.pcap -i -d eth0 'myservice' 'tcp dst port 8280'

How to test web service using command line curl

I am building a web service for a web application, and I would like a simple tool to test this as I am developing. I have tried some firefox plug-ins (Poster, 'REST Client'), and even though these work fine I have been unable to upload files with them.
Also, I would rather have a command-line tool that I can use to easily write a set of integration tests for this web service and that I can send to consumers of this web service as an example.
I know that curl can work for this but would like a few examples, especially around authentication (using HTTP Basic) and file uploads.
Answering my own question.
curl -X GET --basic --user username:password \
https://www.example.com/mobile/resource
curl -X DELETE --basic --user username:password \
https://www.example.com/mobile/resource
curl -X PUT --basic --user username:password -d 'param1_name=param1_value' \
-d 'param2_name=param2_value' https://www.example.com/mobile/resource
POSTing a file and additional parameter
curl -X POST -F 'param_name=#/filepath/filename' \
-F 'extra_param_name=extra_param_value' --basic --user username:password \
https://www.example.com/mobile/resource
In addition to existing answers it is often desired to format the REST output (typically JSON and XML lacks indentation). Try this:
$ curl https://api.twitter.com/1/help/configuration.xml | xmllint --format -
$ curl https://api.twitter.com/1/help/configuration.json | python -mjson.tool
Tested on Ubuntu 11.0.4/11.10.
Another issue is the desired content type. Twitter uses .xml/.json extension, but more idiomatic REST would require Accept header:
$ curl -H "Accept: application/json"
From the documentation on http://curl.haxx.se/docs/httpscripting.html :
HTTP Authentication
curl --user name:password http://www.example.com
Put a file to a HTTP server with curl:
curl --upload-file uploadfile http://www.example.com/receive.cgi
Send post data with curl:
curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi