0

I would like to send a Post Request to a Rest Server of external Provider. I Have tried with Curl and everything works perfect. Here is the CURL Code:

    curl -X POST -H 'PRODUCT-KEY: SUperL0ngAndSpecialSecretCode' -H 'Content-Type: application/json' -H 'Authorization: Basic CrytedWorksalsowellwithotherget' -i 'http://myserver:8080/rest.svc/v1.0/query' --data '{
"query":"SELECT Name from Address where name like '\''%me%'\''"
}'

In Curl Everything works fine. I have tried for a bunch of hours to get this Code working in Delphi. At this time my code looks like this:

function GetSomeInformation: string;
var
  lrestrequest: TRESTRequest;
  lRestClient: TRESTClient;
  lRestResponce: TRESTResponse;
begin
  result := '';
  lRestClient := TRESTClient.Create('http://myserver:8080/rest.svc/v1.0/query');
  try
    lrestrequest := TRESTRequest.Create(nil);
    try
      lRestResponce := TRESTResponse.Create(nil);
      try
        lrestrequest.Client := lRestClient;
        lrestrequest.Response := lRestResponce;
        lrestrequest.Method := rmPost;
        lrestrequest.Params.AddItem('PRODUCT-KEY',
          'SUperL0ngAndSpecialSecretCode',
          TRESTRequestParameterKind.pkHTTPHEADER);
        lrestrequest.Params.AddItem('Content-Type', 'application/json',
          TRESTRequestParameterKind.pkHTTPHEADER);
        lrestrequest.Params.AddItem('query',
          ansitoutf8('SELECT Name from Address where Name like ' +
          quotedstr('%me%')), TRESTRequestParameterKind.pkREQUESTBODY);
        lrestrequest.Execute;
        if not lRestResponce.Status.Success then
          showmessage(lRestResponce.StatusText + ' ' +
            inttostr(lRestResponce.StatusCode))
        else
          result := lRestResponce.Content;
      finally
        lRestResponce.Free;
      end;
    finally
      lrestrequest.Free
    end;
  finally
    lRestClient.Free;
  end;
end;

I have no idea what to do next to get the Work done? Any ideas or ways I can Debug the problem better.

--Update

Okay I Used Wireshark to check if there are any differences between the Post commands, it look like Delphi ignores or broke my Header. In the Wireshark snippet there is a Value Content-Type. It should be

Content-Type: application/json

But with Delphi I get

Content-Type: application%2Fjson, application/x-www-form-urlencoded

And I also miss the Procduct-Key Value. Any Suggestions?

Sam M
  • 4,136
  • 4
  • 29
  • 42
fisi-pjm
  • 378
  • 2
  • 16
  • Sorry forgot to add some Information. The Authentication works with a Basic authenticator, in my "real" Project I use the Components on VCL Form, an I have done other Requests with a GET Value and Authentication, that works great. – fisi-pjm May 30 '18 at 08:34
  • use wireshark/fiddler to compare the CURL request against the TRestRequest request and see whats different. – whosrdaddy May 30 '18 at 11:13

2 Answers2

1

You have to specify the kind of request in Method property of TRESTRequest

lRESTRequest.Method := TRESTRequestMethod.rmPost

Delphi ships with code samples!

http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/REST.RESTDemo_Sample

Erwin
  • 1,896
  • 1
  • 11
  • 17
0

after some Research and some Wireshark I got my Work done at least here ist the way I throw the parameters into my Request.

function GetSomeInformation: string;
var
  lrestrequest: TRESTRequest;
  lRestClient: TRESTClient;
  lRestResponce: TRESTResponse;
begin
  result := '';
  lRestClient := TRESTClient.Create('http://myserver:8080/rest.svc/v1.0/query');
  try
    lrestrequest := TRESTRequest.Create(nil);
    try
      lRestResponce := TRESTResponse.Create(nil);
      try
        lrestrequest.Client := lRestClient;
        lrestrequest.Response := lRestResponce;
        RESTRequest1.Params.Clear;
        RESTRequest1.Method:=rmpost;
        RESTResponse1.RootElement := '';
        lparam := RESTRequest1.Params.AddItem;
        lparam.name := 'PRODUCT-KEY';
        lparam.Value := 'SpecialKeyButWithSomeTrickyCharsLike==';
        lparam.ContentType := ctNone;
        lparam.Kind := pkHTTPHEADER;
        //This one is Important otherwise the '==' will get url encoded
        lparam.Options := [poDoNotEncode];

        lparam := RESTRequest1.Params.AddItem;
        lparam.name := 'data';
        lparam.Value := '{"query":"' + SelectString + '"}';
        lparam.ContentType := ctAPPLICATION_JSON;
        lparam.Kind := pkGETorPOST;
        lrestrequest.Execute;
        if not lRestResponce.Status.Success then
          showmessage(lRestResponce.StatusText + ' ' +
            inttostr(lRestResponce.StatusCode))
        else
          result := lRestResponce.Content;
      finally
        lRestResponce.Free;
      end;
    finally
      lrestrequest.Free
    end;
  finally
    lRestClient.Free;
  end;
end;

Thanks for Supporting!

PJM

Daniel Grillo
  • 2,368
  • 4
  • 37
  • 62
fisi-pjm
  • 378
  • 2
  • 16