分享

Calling REST Web API using Delphi

 quasiceo 2016-12-15

I have an ASP.NET MVC Web API and I need to call it using Delphi 6. I am trying to use the Indy components (version 9.0.18), I am using the TIdHttp component.

I am using the REST methods, like POST to add, PUT to update and DELETE to delete the records. I got successfully adding, updating and getting my records, but I couldn't get successfully to call the DELETE method. It raises an error "HTTP/1.1 400 Bad Request".

I tried to debug the Web API, but it appers that the request didn't come, cause it doesn't stop in the breakpoint.

The Indy version that I am using doesn't have the method DELETE, so I tried to use the DoRequest method.

My code:

IdHTTP.DoRequest(hmDelete, 'http://localhost/myapp/api/user/1', nil, nil);

If I make the request using Fiddler it works, so my Web API is working well.

asked Feb 16 at 19:15

    
Sounds like you need to do some debugging. I'd use a tool like Wireshark to inspect the requests. What's your question for Stack Overflow? – Rob Kennedy Feb 16 at 22:18
    
If I find a way to inspect the requests will help me to undertand the problem. I will try to use the Wireshark. But my question is how to make a DELETE request using Indy components. Does Anyone already do it? – Rodrigo Kiguti Feb 17 at 10:34
    
The problem is that I need to use an old version on Delphi and Indy, and I can't update. I know newer versions has better support to make REST requests. – Rodrigo Kiguti Feb 17 at 10:38
    
New Indy versions can be used with Delphi 6 (and 5) – mjn Feb 17 at 16:09
    
You can use Wireshark or Fiddler to compare a working Delete request with the non-working to find the difference – mjn Feb 17 at 16:10
up vote 1 down vote accepted

As an alternative to Indy, I suggest you use "Import Type Library..." from the Project menu, and select "Microsoft XML", the highest version available (I have versions 3 through 6 on the machine I'm currently on). Disable "Generate Component Wrapper", and then use the XMLHTTP component to make REST calls. E.g.:

uses ActiveX, MSXML2_TLB;

var
  r:XMLHTTP;
begin
  CoInitialize(nil);
  r:=CoXMLHTTP.Create;
  r.open('DELETE','http://localhost/myapp/api/user/1',false,'','');
  //r.setRequestHeader(...
  r.send(EmptyParam);
  if r.status=200 then
answered Feb 17 at 10:58
Stijn Sanders
15.5k63148

    
It works! Cause I have another question. What is the better version of the MSXML to use? Because I have customers using Windows XP through Windows 10. Which version has the best compatibility? – Rodrigo Kiguti Feb 17 at 11:54
    
In theory the XMLHTTP interface declarations have been there since version 3, and the live systems puts your calls through to any msxml2.dll that happens to be there, so with any version since Windows 2000 and IE since 5.5 it should work as expected. – Stijn Sanders Feb 17 at 12:15
    
The theory appears to work. I imported the library from MSXML 4.0 and I tested in a virtual machine that doesn't have version 4.0 installed and it works. – Rodrigo Kiguti Feb 17 at 12:56

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多