You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Delphi TRestClient以Postman方式发送raw请求体?

Delphi TRestClient 实现raw形式发送application/x-www-form-urlencoded请求体

要让TRestClient像Postman的--data-raw那样发送原始格式的application/x-www-form-urlencoded请求体,需要绕过TRestRequest默认的表单参数解析逻辑,具体操作如下:

核心原理

TRestRequest默认会将application/x-www-form-urlencoded类型的请求体拆分为键值对并自动编码,而我们需要直接发送完整的原始字符串,因此需要修改参数的内容类型配置,避免自动解析。

具体实现代码

var
  RestClient: TRESTClient;
  RestRequest: TRESTRequest;
begin
  // 初始化RestClient,指定目标接口地址
  RestClient := TRESTClient.Create('https://ms2.medapp.ch/rest/lg1/v1/call-medapp');
  try
    RestRequest := TRESTRequest.Create(nil);
    try
      RestRequest.Client := RestClient;
      RestRequest.Method := rmPOST;

      // 设置请求头Content-Type
      RestRequest.AddParameter('Content-Type', 'application/x-www-form-urlencoded', pkHTTPHEADER);

      // 关键配置:将参数内容类型设为文本,禁止自动解析表单参数
      RestRequest.ParameterContentType := ctTEXT;

      // 添加原始请求体字符串,参数类型为请求体
      RestRequest.AddParameter('..datastring..', pkREQUESTBODY);

      // 执行请求
      RestRequest.Execute;

      // 此处可添加响应处理逻辑,例如获取响应内容
      // ShowMessage(RestRequest.Response.Content);
    finally
      RestRequest.Free;
    end;
  finally
    RestClient.Free;
  end;
end;

关键说明

  • 设置RestRequest.ParameterContentType := ctTEXT是核心:这个配置会告诉TRestRequest不要对请求体参数进行表单解析和编码,直接将传入的字符串作为原始内容发送,和curl的--data-raw行为完全一致。
  • 确保传入的..datastring..本身是符合application/x-www-form-urlencoded格式的字符串(例如key1=value1&key2=value2,特殊字符已提前编码),因为此时框架不会再自动处理编码逻辑。

内容的提问来源于stack exchange,提问作者F. Zeller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:13:22