Hello everyone.
I have an ESP32 with the arduino Ethernet shield. With it, I am trying to send out a HTTP Get request to a server to get a file!
In the URL, I have parameters such as a token to access the server. I am currently using ArduinoHttpClient library.
I have everyhting mostly setup. Just wanting to know if this is the correct method on handling URL parameters. Basically, what I am doing is that I am extracting out the parameter and the value (Key Value pair essentially)
Here is my code:
URLKVPair keyValuePairs[AMAZON_AWS_URL_PARAMETER_COUNT];
String baseURL;
String response[2];
File EEPROMFile;
parseAmazonURL(server, baseURL, keyValuePairs);
int sectionIndex = baseURL.indexOf("//", 8); // Need to find the second one
response[0] = baseURL.substring(8, sectionIndex);
response[1] = baseURL.substring(sectionIndex + 1, baseURL.length());
client->beginRequest();
client->get(response[1]);
keyValuePairs[2].value.replace("%2F", "/");
keyValuePairs[5].value.replace("%2F", "/");
keyValuePairs[5].value.replace("%2B", "+");
String creds = keyValuePairs[2].value;
String algorithmAmazon = keyValuePairs[0].value;
String amazonSignature = keyValuePairs[6].value;
String authorizationHeader = "Authorization: " + algorithmAmazon + " Credential=" + creds + ", SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-expires, Signature=" + amazonSignature;
Serial.println("Auth Header:");
Serial.println(authorizationHeader);
client->sendHeader(authorizationHeader); // The Authorization header is as follows: Authorization: AWS4-HMAC-SHA256 Credential=<credValue>/20240212/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=<Signature value>
client->sendHeader(keyValuePairs[1].key, keyValuePairs[1].value); // This header is this value: X-Amz-Content-Sha256=UNSIGNED-PAYLOAD
client->sendHeader(keyValuePairs[3].key, keyValuePairs[3].value); // This header is this value: X-Amz-Date=20240212T003504Z
client->sendHeader(keyValuePairs[5].key, keyValuePairs[5].value); // This is the x-amz-security-token
client->sendHeader(keyValuePairs[4].key, keyValuePairs[4].value);
client->endRequest();
int statusCode = client->responseStatusCode();
For reference, here is the output of the KV pair function that extracts out the parameters (redacting the more sensitive information)
Key: X-Amz-Algorithm, Value: AWS4-HMAC-SHA256
Key: X-Amz-Content-Sha256, Value: UNSIGNED-PAYLOAD
Key: X-Amz-Credential, Value: Cred_Value_Here
Key: X-Amz-Date, Value: 20240202T000432Z
Key: X-Amz-Expires, Value: 43200
Key: X-Amz-Security-Token, Value: Token_Value_Here
Key: X-Amz-Signature, Value: Signature_Value_here
Key: X-Amz-SignedHeaders, Value: host
Key: x-id, Value: GetObject
Now I am getting back a response value that says that the signature is not correct.
I did some online searching and found out that I need to get an Authorization header with the required information per Amazon spec..
My question here is this, is there an Arduino library that implements the HTTP GET request that can take URL parameters? If I take my URL and place it in a web browser, I am able to access the file no problem.
If this doesn't exist, then is there a library for the ESP32 that can connect to Amazon services to download a file?
Please let me know.