从 MimeType application/pdf 的 TNetHttpClient 获取响应
我在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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将二进制PDF文件视为UTF-8字符串,这就是为什么
contentAsstring()
在编码错误中失败的原因。per 文档:
因此,这些方法中的任何一种都可以正常工作:
如果它们不适合您,则需要文件错误报告带有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:So, either of these approaches should work fine:
If they do not work for you, you will need to file a bug report with Embarcadero.