从 MimeType application/pdf 的 TNetHttpClient 获取响应

发布于 2025-01-19 13:37:01 字数 869 浏览 0 评论 0原文

我在Delphi中调用API,其响应是PDF文件。我正在使用Mimetype Application-PDF获得IHTTPRESPONSE。如何从此响应中创建一个PDF文件?

代码:

response := Form1.NetHTTPClient1.Post(apiurl,parametres, nil, headerparams);
responsestring := response.ContentAsString(tencoding.UTF8);
Form1.memo.Lines.Add(responsestring);

当我尝试将响应转换为contentAsstring以下错误即将到来:

“在此处输入图像说明”

我什至尝试在邮政请求中传递tstream对象:

response := Form1.NetHTTPClient1.Post(apiurl,parametres, resStream, headerparams);
responsestring := response.ContentAsString(tencoding.UTF8);
Form1.memo.Lines.Add(responsestring);

但是Resstream的值是''后通话后。响应代码即将推出200,这意味着我得到了回应。

在我尝试此操作时,在Postman中,我会在响应中获得一个PDF文件。

I am calling an API in Delphi the response of which is a pdf file. I am getting IHttpResponse, with MimeType application-pdf. How can I create a pdf file from this response?

Code:

response := Form1.NetHTTPClient1.Post(apiurl,parametres, nil, headerparams);
responsestring := response.ContentAsString(tencoding.UTF8);
Form1.memo.Lines.Add(responsestring);

When I try to convert the response to ContentAsString the below error is coming:

enter image description here

I even tried to pass a TStream Object in the post request:

response := Form1.NetHTTPClient1.Post(apiurl,parametres, resStream, headerparams);
responsestring := response.ContentAsString(tencoding.UTF8);
Form1.memo.Lines.Add(responsestring);

But the value of resStream is '' after Post call. The response code is coming 200 which means I am getting a response.

In Postman when I try this, I get a pdf file in response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最佳男配角 2025-01-26 13:37:01

您不能将二进制PDF文件视为UTF-8字符串,这就是为什么contentAsstring()在编码错误中失败的原因。

per 文档:

如果您想接收响应数据,因为您的HTTP客户端从目标服务器下载它,而不是等待您的HTTP客户端下载整个数据,而是使用AresponSecontent参数来指定a << a href =“ https://docwiki.embarcadero.com/libraries/en/system.classes.tstream” rel =“ nofollow noreferrer”> stream 接收下载的数据。另外,您可以等待您的HTTP客户端下载整个响应数据,并从响应对象 [post]返回。

因此,这些方法中的任何一种都可以正常工作:

response := Form1.NetHTTPClient1.Post(apiurl, parametres, nil, headerparams);
fs := TFileStream.Create('path\output.pdf', fmCreate);
try
  fs.CopyFrom(response.ContentStream, 0);
finally
  fs.Free;
end;
fs := TFileStream.Create('path\output.pdf', fmCreate);
try
  Form1.NetHTTPClient1.Post(apiurl, parametres, fs, headerparams);
finally
  fs.Free;
end;

如果它们不适合您,则需要文件错误报告带有Embarcadero。

You can't treat a binary PDF file as a UTF-8 string, which is why ContentAsString() is failing with an encoding error.

Per the TNetHttpClient.Post() documentation:

If you want to receive the response data as your HTTP client downloads it from the target server, instead of waiting for your HTTP client to download the whole data, use the AResponseContent parameter to specify a stream to receive the downloaded data. Alternatively, you can wait for your HTTP client to download the whole response data, and obtain the response data as a stream from the ContentStream property of the response object that [Post] returns.

So, either of these approaches should work fine:

response := Form1.NetHTTPClient1.Post(apiurl, parametres, nil, headerparams);
fs := TFileStream.Create('path\output.pdf', fmCreate);
try
  fs.CopyFrom(response.ContentStream, 0);
finally
  fs.Free;
end;
fs := TFileStream.Create('path\output.pdf', fmCreate);
try
  Form1.NetHTTPClient1.Post(apiurl, parametres, fs, headerparams);
finally
  fs.Free;
end;

If they do not work for you, you will need to file a bug report with Embarcadero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文