I am using SignatureV4 aws authentication to authenticate my API call, I am also using Guzzle/Client to call API.

By following these above, my API call works for GET method only and not working for POST method. For POST method call it show an error ( The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method )

My code :

use Aws\Credentials\CredentialProvider;
use Aws\Signature\SignatureV4;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$request = new Request(
    'POST',
    'https://XXXXXXXXXXXXx.com/XXXXXx/XXXXXXx',
    [
        'body' => '{
            "param1": "loream",
            "param2": "4970",
            "param3": "1",
            "param4": "2.0",
            "param5": "mL",
            "param6": "kgs",
            "param7": 0,
        }'
    ]      
);

$key['credentials'] = array( 
    'key'    => 'XXXXXXXXXXXXXXX-access-key',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-secret-key'
);

$credentials = call_user_func(CredentialProvider::defaultProvider(
    $key
));

// Construct a request signer
$signer = new SignatureV4('execute-api', 
                        'us-east-1'
);

// Sign the request
$request = $signer->signRequest($request, $credentials);

// Send the request
try {
    $response = (new Client)->send($request);
    print_r($response);
}
    catch (Exception $exception) {
    $responseBody = $exception->getResponse()->getBody(true);
    echo $responseBody;
}
$response = (new Client)->send($request);

$results = json_decode($response->getBody());
0

There are 0 best solutions below