简体   繁体   English

如何在Delphi 7中使用DELETE方法和JSON流源发送Indy请求?

[英]How to send Indy request with DELETE methode and JSON stream source in Delphi 7?

I have a problem in sending web service request using Delphi 7 and Indy. 我在使用Delphi 7和Indy发送Web服务请求时遇到问题。 My workplace a public hospital as a client and insurance third party as the server. 我的工作场所以公立医院为客户,以保险第三方为服务器。 Here is the service catalogue: 这是服务目录:

URL : {BASE URL from the insurance office}/Delete
    Method : DELETE
    Format : Json
    Content-Type: Application/x-www-form-urlencoded
    Request body : 
        {"request": {"t_obj": {"noObj": "0301X1018V001","user": "myUser"}}}
I use Indy 10.6 and some of the code I wrote is: 我使用Indy 10.6,我写的一些代码是:

  Json := '{"request": {"t_obj": {"noObj": "0301X1018V001","user": "myUser"}}}'; req := TStringStream.Create(Utf8Encode(Json)); resp := TStringStream.Create(''); IdHttp1.request.Source := req; IdHttp1.Request.ContentType := 'Application/x-www-form-urlencoded'; IdHttp1.delete('{BASE URL from the insurance office}/Delete', resp); showmessage(resp.DataString); 

But, when the request sent, it failed to delete. 但是,发送请求后,它无法删除。 Any one can help me, please? 有人可以帮助我吗? I'm sorry my English is not good enough. 对不起,我的英语还不够好。 Thank you. 谢谢。

Application/x-www-form-urlencoded is not a valid media type for sending JSON. Application/x-www-form-urlencoded不是用于发送JSON的有效媒体类型。 Are you sure the server is not actually expecting application/json instead? 您确定服务器实际上并不期望使用application/json吗? It should be. 它应该是。

Beside that, a more important reason why your request does not work is because the TIdHTTP.Delete() method simply does not allow sending a post body, so the server never sees the JSON at all. 除此之外,您的请求不起作用的一个更重要的原因是因为TIdHTTP.Delete()方法根本不允许发送帖子正文,因此服务器根本看不到JSON。 Internally, Delete() calls the TIdCustomHTTP.DoRequest() method passing nil in the ASource parameter, which replaces your assignment of the TIdHTTP.Request.Source property. 在内部, Delete()调用TIdCustomHTTP.DoRequest()方法,并在ASource参数中传递nil ,该方法将替换您对TIdHTTP.Request.Source属性的分配。

To do what you are attempting, you will have to call DoRequest() directly, eg: 要执行您要尝试的操作,您将必须直接调用DoRequest() ,例如:

// TIdCustomHTTP.DoRequest() is protected,
// so use an accessor class to reach it...
type
  TIdHTTPAccess = class(TIdHTTP)
  end;

...

Json := '{"request": {"t_obj": {"noObj": "0301X1018V001","user": "myUser"}}}';
req := TStringStream.Create(UTF8Encode(Json));
try
  resp := TStringStream.Create('');
  try
    //IdHttp1.Request.Source := req;
    //IdHttp1.Request.ContentType := 'Application/x-www-form-urlencoded';
    IdHttp1.Request.ContentType := 'application/json';
    //IdHttp1.Delete('{BASE URL from the insurance office}/Delete', resp);
    TIdHTTPAccess(IdHttp1).DoRequest(Id_HTTPMethodDelete, '{BASE URL from the insurance office}/Delete', req, resp, []);
    ShowMessage(resp.DataString);
  finally
    resp.Free;
  end;
finally
  req.Free;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM