TsgcHTTP2ClientMethods › Post

Post 方法

执行同步 HTTP/2 POST,上传流并阻塞直到收到响应。

重载

重载 1

语法

function Post(const aURL: string; const aSource: TStream): string;

参数

名称类型描述
aURLconst string将接收 POST 请求的绝对 URL。
aSourceconst TStream包含要上传的请求体的流。其 Size 设置 content-length 伪头。

返回值

从服务器接收的响应体,解码为字符串。(string

备注

对于可以以字符串形式返回完整响应的文本交换(如 JSON 或表单数据),请使用此重载。在调用之前配置 Request.ContentType 以声明消息体的媒体类型。

示例

oBody := TStringStream.Create('{"name":"John"}');
try
  oClient.Request.ContentType := 'application/json';
  vResponse := oClient.Post('https://api.example.com/users', oBody);
finally
  oBody.Free;
end;

重载 2

语法

procedure Post(const aURL: string; const aSource: TStream; const aResponseContent: TStream);

参数

名称类型描述
aURLconst string将接收 POST 请求的绝对 URL。
aSourceconst TStream提供请求载荷的流。
aResponseContentconst TStream将接收服务器原始响应字节的流;适用于二进制或大型响应。

备注

完全基于流的变体:请求载荷和服务器响应都通过 TStream 实例处理,避免了两端字符串转换的开销。对于返回二进制载荷的文件上传,首选此重载。

示例

oIn := TFileStream.Create('upload.bin', fmOpenRead);
oOut := TMemoryStream.Create;
try
  oClient.Post('https://api.example.com/upload', oIn, oOut);
finally
  oOut.Free;
  oIn.Free;
end;

返回方法