[英]Calling REST Web API using Delphi
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.
我正在使用 REST 方法,例如 POST 添加、PUT 更新和 DELETE 删除记录。 我成功添加、更新和获取我的记录,但我无法成功调用 DELETE 方法。 它会引发错误“HTTP/1.1 400 Bad Request”。
我试图调试 Web API,但它似乎请求没有到来,因为它没有在断点处停止。
我使用的 Indy 版本没有 DELETE 方法,所以我尝试使用 DoRequest 方法。
我的代码:
IdHTTP.DoRequest(hmDelete, 'http://localhost/myapp/api/user/1', nil, nil);
如果我使用 Fiddler 发出请求,它可以工作,所以我的 Web API 运行良好。
作为 Indy 的替代方案,我建议您使用“项目”菜单中的“导入类型库...”,然后选择可用的最高版本“Microsoft XML”(我当前使用的机器上有 3 到 6 版) . 禁用“生成组件包装器”,然后使用XMLHTTP
组件进行 REST 调用。 例如:
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
所以另一个答案很简单,使用具有灵活后期绑定的 COM-Object,例如 REST 翻译服务,在 GET 或 POST 中进行语言检测,在 maXbox 脚本中实现:
function getPostTranslateLibre(feedstream: string): string;
var
Url,API_KEY, source: string;
jo, locate: TJSONObject;
httpReq,hr: Olevariant;
strm: TStringStream;
begin
httpReq:= CreateOleObject('WinHttp.WinHttpRequest.5.1');
// Open the HTTPs connection.
try
hr:= httpReq.Open('POST','https://libretranslate.pussthecat.org/detect', false);
httpReq.setRequestheader('user-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0');
httpReq.setRequestheader('content-type','application/x-www-form-urlencoded');
//httpReq.setRequestheader('X-RapidAPI-Host','nlp- translation.p.rapidapi.com');
//httpReq.setRequestheader('X-RapidAPI-Key','...333');
if hr= S_OK then HttpReq.Send('q='+HTTPEncode(feedstream));
/// Send HTTP Post Request & get Responses.
If HttpReq.Status = 200 Then
result:= HttpReq.responseText
Else result:= 'Failed at getting response:'+itoa(HttpReq.Status)+HttpReq.responseText;
//writeln('debug response '+HttpReq.GetAllResponseHeaders);
finally
httpreq:= unassigned;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.