简体   繁体   中英

How to translate CURL command to Delphi

I'm trying to use Delphi 10.2 TREST components with AWS. I have a CURL command that works:

curl -X POST --data @GetIDData.json -H "X-Amz-Target: AWSCognitoIdentityService.GetId" -H "Content-Type: application/x-amz-json-1.1" https://cognito-identity.us-east-1.amazonaws.com/

GetIDData.json contains this:

{"IdentityPoolId":"us-east-1:XXXXXXXXXXXXXXXXXXXXX"}

Successful result is this:

{"IdentityId":"us-east-1:XXXXXXXXXXXXXXXXXXXXX"}

I'd like to duplicate that result using Delphi TREST components:

    ...
    fClient := TRESTClient.Create('https://cognito-identity.us-east-1.amazonaws.com/');
    fClient.SetHTTPHeader('Content-Type', 'application/x-amz-json-1.1');
    fClient.SetHTTPHeader('X-Amz-Target', 'AWSCognitoIdentityService.GetId');
    fRequest := TRESTRequest.Create(nil);
    fRequest.Client := fClient;
    fRequest.Method := TRESTRequestMethod.rmPOST;
//    fRequest.AddBody('{"IdentityPoolId":"us-east-1:XXXXXXXXXXXXXXXXXXXXX"}', ctAPPLICATION_JSON);
    lJObj := TJSONObject.Create;
    lJObj.AddPair('IdentityPoolId', 'us-east-1:XXXXXXXXXXXXXXXXXXXXX');
    fRequest.AddBody(lJObj);
    fRequest.Execute;
    str := fRequest.Response.Content;
    ...

But the result is an error:

{"Output":"__type":"com.amazon.coral.service#UnknownOperationException","message":null},"Version":"1.0"}

Downloading OpenSSL and putting the dlls into System32 did not help.

Can anyone tell me what I'm doing wrong?

This works:

...
    lClient := TRESTClient.Create('https://cognito-identity.us-east-1.amazonaws.com/');
    lRequest := TRESTRequest.Create(nil);
    lRequest.Client := lClient;
    lRequest.Method := TRESTRequestMethod.rmPOST;

    lParam := lRequest.Params.AddItem;
    lParam.name := 'X-Amz-Target';
    lParam.Value := 'AWSCognitoIdentityService.GetId';
    lParam.ContentType := ctNone;
    lParam.Kind := pkHTTPHEADER;

    lParam := lRequest.Params.AddItem;
    lParam.name := 'Content-Type';
    lParam.Value := 'application/x-amz-json-1.1';
    lParam.ContentType := ctNone;
    lParam.Kind := pkHTTPHEADER;
    lParam.Options := [poDoNotEncode];

    lRequest.AddBody('{"IdentityPoolId":"us-east-1:XXXXXXXXXXXXXXXXXXXXX"}', ctAPPLICATION_JSON);

    lRequest.Execute;
...

WireShark was not as helpful as I wanted because its doc is out of date and I'm using encryption. But the website mentioned by @Christophe Morio in this post made finding a solution a piece of cake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM