TsgcHTTP2Client › Methods › Post
执行同步 HTTP/2 POST,上传流并阻塞直到收到响应。
function Post(const aURL: string; const aSource: TStream): string;
| 名称 | 类型 | 描述 |
|---|---|---|
aURL | const string | 将接收 POST 请求的绝对 URL。 |
aSource | const 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;
procedure Post(const aURL: string; const aSource: TStream; const aResponseContent: TStream);
| 名称 | 类型 | 描述 |
|---|---|---|
aURL | const string | 将接收 POST 请求的绝对 URL。 |
aSource | const TStream | 提供请求载荷的流。 |
aResponseContent | const 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;