Delphi RESTClient POST请求

时间:2015-07-09 14:02:59

标签: rest delphi delphi-xe6

好吧,我正在开发一个REST客户端应用程序,需要使用application/x-www-form-urlencoded作为内容类型发送POST请求。我正在使用Delphi的默认REST.Client组件。我需要以XML格式发送数据,如下例所示:

    data=<serviceLocal>
    <description>Francisco Hansen</description>
    <active>true</active>
    <corporateName>Francisco Hansen</corporateName>
    <country>Brasil</country>
    <state>PR</state>
    <city>Pato Branco</city>
    <cityNeighborhood>Centro</cityNeighborhood>
    <streetType>Rua</streetType>
    <street>Tocantins</street>
    <streetNumber>2334</streetNumber>
    <streetComplement>Ap101</streetComplement>
    <zipCode>85501-272</zipCode>
    <cellphoneStd>46</cellphoneStd>
    <cellphoneNumber>99999999</cellphoneNumber>
    <phoneStd>46</phoneStd>
    <phoneNumber>99999999</phoneNumber>
    </serviceLocal>

如何将所有这些作为POST参数添加到TRestRequest,然后使用TRestClient发送此请求?

1 个答案:

答案 0 :(得分:1)

使用Rest.Client.TRESTRequest组件,我已经能够为请求设置POST参数。我将TRESTRequest命名为rqst1

rqst1.Method := rmPost;

rqst1.Params.AddItem; //Adds a new Parameter Item
rqst1.Params.Items[0].name := 'data'; //sets the name of the parameter. In this case, since i need to use 'data=' on the request, the parameter name is data.
rqst1.Params.Items[0].Value := params; //Adds the value of the parameter, in this case, the XML data.
rqst1.Params.Items[0].ContentType := ctAPPLICATION_X_WWW_FORM_URLENCODED; //sets the content type.
rqst1.Params.Items[0].Kind := pkGETorPOST; //sets the kind of request that will be executed.