I'm getting "The request signature we calculated does not match the signature you provided" error for the following Code used in the Karate framework. Please help me to fix it.
My Feature File:
Feature: Functional validations for API
Background:
* configure headers = { Content-Type: 'application/json',x-api-key: 'apikeyValue' }
Scenario: Verify if API response is success
Scenario Outline: Initial test
* def userdir = karate.properties['user.dir']
* def reusableMethod = function() { var JavaDemo = Java.type('runner.Signature'); jd = new JavaDemo(); return jd.getSignature(); }
* def reusableMethod2 = function() { var JavaDemo2 = Java.type('runner.Signature'); jd = new JavaDemo2(); return jd.getDateString(); }
* def token = call reusableMethod
* def date = call reusableMethod2
Given url baseUrl
And header Authorization = token
And header X-Amz-Date = date
* def requestjson = read('file:' +userdir+'<readRequest>')
When request requestjson
And set requestjson.header.applicationId = '<applicationId>'
And method POST
Then status 200
Examples:
| readRequest | applicationId |
| /src/test/java/resources/input.json | abc |
My runner.Signature Class:
public String getDateString() {
Calendar cal = Calendar.getInstance();
DateFormat dfm = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
dfm.setTimeZone(TimeZone.getTimeZone("UTC")); //server timezone
return dfm.format(cal.getTime());
}
public String getSignature() throws URISyntaxException {
String contentSha256 = "sampleValue";
String ACCESS_KEY = "accesskeyValue";
String SECRET_KEY = "SecretKeyValue";
HttpRequest request = new HttpRequest("GET", new URI("UrlValue"));
AwsCredentials awscred = new AwsCredentials(ACCESS_KEY, SECRET_KEY);
String signature1 = Signer.builder()
.awsCredentials(awscred)
.region("regionValue")
.header("content-type","application/json")
.header("host", "HostValue")
.header("x-api-key", "apikeyValue")
.header("x-amz-date", getDateString())
.header("x-amz-content-sha256", contentSha256)
.build(request,"execute-api", contentSha256)
.getSignature();
return signature1;