Delpheloper
Delpheloper

Reputation: 51

Delphi and Webservices

I'm developing a tool to integrate two systems and I need some help regarding Delphi and Webservices.

First of all, I'm working with Delphi 5.0 and Indy 8.009B. I'm aware that they are both ancient, but that's what I have to deal with for this moment.

The first problem I had was about retrieving a token. But I managed to do it by finding the right DLL files to work with my Indy's IdHTTP component version (the 2004 ones by the way).

Here is my code:

slParams := TStringList.Create;
  sReturnWS := TStringStream.Create('');
  cIntercept := TIdConnectionInterceptOpenSSL.Create(nil);
  sParams := 'client_id=' + sIdClient + '&client_secret=' + sClientKey;
  sURL := sUrlAut + '?' + sParams;
  IdHTTP := TIdHTTP.Create(nil);
  IdHTTP.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
  cIntercept.SSLOptions.Method := sslvTLSv1;
  IdHTTP.Request.Accept := '*/*';
  IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
  IdHTTP.Request.ContentEncoding := 'utf-8';
  IdHTTP.Intercept := cIntercept;
  IdHTTP.ProtocolVersion := pv1_0;
  IdHTTP.Request.Connection := 'keep-alive';
  IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
  try
    IdHTTP.Post(sURL, slParams, sReturnWS);
    sReturn := sReturnWS.DataString;
    vJson := TlkJSON.ParseText(sReturn);

It works like a charm. But now I'm currently stuck at the second step, which is sending the server the received token through Bearer Token authorization/authentication. I dinamically create the json I'm supposed to send to the server, which is a HTTPS secure server by the way. The json file is just fine, I've tested it with online json validators by saving it from the memory stream to the hard drive and I have also tested it using both Postman and SoapUI and it works and gets validated just fine. But when I try to send it through my Delphi application I always get the same error: 422 Unprocessable Entity which is driving me crazy because that's all I can read from the server's response. It's not like Postman or SoapUI where I can read all the returned message from the server. Well to summon it up I believe I'm having this error because of some necessary authorization that I'm not setting up correctly through Delphi; Anyway, here is my code to send the json file (json is my string variable containing the json data):

JsonToSend.WriteBuffer(Pointer(Json)^, Length(Json));
JsonToSend.Position := 0;
cIntercept := TIdConnectionInterceptOpenSSL.Create(nil);
try
    IdHTTPAux.Intercept := cIntercept;
    IdHTTPAux.Request.Clear;
    IdHTTPAux.Request.ContentType := 'application/json';
    IdHTTPAux.Request.ContentEncoding := 'UTF-8';
    IdHTTPAux.Request.ExtraHeaders.Clear;
    IdHTTPAux.Request.ExtraHeaders.FoldLines := False;
    IdHTTPAux.Request.WWWAuthenticate := 'Bearer TOKEN';
    idHTTPAux.Request.ExtraHeaders.Values['Cache-Control'] := 'no-cache';
    IdHTTPAux.Request.ExtraHeaders.Values['Authorization'] := 
        Uppercase(copy(sTokenType,1,1)) + copy(sTokenType,2,length(sTokenType)-1) + ' ' + 
        sToken;
    IdHTTPAux.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
    cIntercept.SSLOptions.Method := sslvTLSv1;
    IdHTTPAux.Request.Accept := '*/*';
    IdHTTPAux.ProtocolVersion := pv1_0;
    IdHTTPAux.Request.Connection := 'Keep-Alive';
    IdHTTPAux.Request.AcceptEncoding := 'gzip, deflate, br';
    try
        IdHTTPAux.Post(sUrl, JsonToSend, sResponse);

Someone please help me because I'm stuck on this for days, I've tried everything in my power and I don't know what to do anymore.

Upvotes: 2

Views: 783

Answers (2)

Andrew
Andrew

Reputation: 113

In case you aren't aware, theres an free product called mitmproxy which works well for looking into your https traffic for debugging. Its at http://mitmproxy.org

Upvotes: 1

Delpheloper
Delpheloper

Reputation: 51

I solved it. In fact at some point after I have validated my dinamically generated json I have accidentally deleted the last "}" character from it. The code in itself is fine. It's working 100% now. So if anyone needs an example on how to integrate systems using json and webservice using Delphi 5 and Indy 8.009B there it is. It works!

Upvotes: 3

Related Questions