AWS API V4 request with API key failing for HTTP calls from ruby

I am able to get successful response from AWS V4 API with api key using CURL with syntax:

curl --location '' \ --header 'x-api-key: api-key' But when I implement the HTTP request for the same I get 401. Replaced actual URL with and actual api-key with api-key

require "uri"
require "net/http"

url = URI("")

https =, url.port)
https.use_ssl = true

request =
request["x-api-key"] = api_key

response = https.request(request)

Any suggestions about why a curl request is successful and the same request using Ruby HTTP libraries return 401.

I haven’t faced an issue like this before. I have tried the same request using Net::HTTp, HTTParty and RestClient in ruby.