如何将CURL命令转换为Delphi

时间:2019-05-03 17:59:58

标签: rest curl delphi-10.2-tokyo

我正在尝试在AWS中使用Delphi 10.2 TREST组件。我有一个可以使用的CURL命令:

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包含以下内容:

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

成功的结果是这样:

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

我想使用Delphi TREST组件复制该结果:

    ...
    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;
    ...

但是结果是一个错误:

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

下载OpenSSL并将dll放入System32并没有帮助。

有人可以告诉我我在做什么错吗?

谢谢

大卫

1 个答案:

答案 0 :(得分:0)

这有效:

...
    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并没有我想要的那样有用,因为它的文档已过时并且我正在使用加密。但是@Christophe Morio在this帖子中提到的网站使得找到解决方案变得轻而易举。