Save pdf using HTTP request

Hello,

I have a problem saving a pdf file using HTTP activity.
I received a response message

-MIME_boundary
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: 8bit
Content-ID: <m21321231dasdasdasd@apache.com>

<?xml version='1.0' ?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<con1:DocumentAttachment xmlns:con1="localhost/test">

<con1:Name>001</con1:Name>
<con1:MimeType>application/pdf</con1:MimeType>
<con1:Length>54005</con1:Length>
<SOAP-ENV:Content>
<theSignedForm href="cid:m21321231dasdasdasd@apache.com"/>
</SOAP-ENV:Content>
</SOAP-ENV:Envelope>

--MIME_boundary
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: <m21321231dasdasdasd@apache.com>


%PDF
%PDF-1.5
%????
4 0 obj
<</Subtype/Image/Width 1240/Height 1754>>
stream
//String binary  pdf
--MIME_boundary--

In properties (ResourcePath ) gives directory path
example:
c:\test.pdf

but when I open the file I get a message that the file is damaged

Hi @mican

Could you show us an example of all the settings you have set for the HTTP Request activity? Please try that for a publicly accessible PDF first.

The problem is caused by the xml content with the header. When we remove this content, the pdf file displays correctly.

The question is whether the Resourcepath content can be written to a string variable?
Because if I do this with a Result variable, the binary string is different from the ResourcePath (probably different encoding) and then the file is damaged

Hi @mican

It would be awesome if we could reproduce this error on our end. Any chance you could provide help here (i.e. a project that reproduces this issue shared via a private message)?

One workaround, although not the greatest one, would be to perform the HTTP Request ‘manually’ by using some code and the Invoke Code activity.

This is quite rough, but it worked for me for one project:

Dim myReq As System.Net.HttpWebRequest
Dim myResp As System.Net.HttpWebResponse

myReq = System.Net.HttpWebRequest.CreateHttp("URL")

myReq.Method = "GET"
myResp = DirectCast(myReq.GetResponse, System.Net.HttpWebResponse)
statusCode = myResp.StatusCode
statusDescription = myResp.StatusDescription
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
myText = myreader.ReadToEnd

I cannot share project because they contain sensitive data but I describe issue below:

sample HTTP request:

I get response ResultResponse(string variable):

After saving the test.pdf (using ResourcePath) file to local disk, its contents are the same message as ResultResponse

After opening the file, a message pops up saying the file is damaged

If I remove part of the header from the file (manually), the file will display correctly

I tried to extract message (regex) starting from% PDF to %% EOF and convert to array byte,but it didn’t help.

Indeed. Any chance you can reproduce this error with a publicly available PDF found on Google?

I ask it because for this sample project here (a PDF of 5 mb), it works correctly:
TestingPDFDownload.zip (1.8 KB)

Downloading files does not work only if I use the SOAP service. Others work.

If I find public access to the SOAP service where I can download files, I will insert it here with an example

I have to say, I don’t have much experience here (using HTTP Request to send out a SOAP Request).

For that, I would suggest using our Library services option, see our documentation here:

It should be much more robust.

Hi,
I’ve got a similar problem. A bot upload the file (in my case it’s an Outlook message) to an external system. The uploading runs well. However when I download (manually) the file on my PC I cannot open it because the file is corrupted. On top and at the bottom of the file are added some rows like you can see. I’m curious to understand how you have solved it.
Thanks for your help

Hi,

I didn’t solve this problem. You have a similar problem, but you use email messages and I use SOAP service.

To solve your problem please use the link below:

https://forum.uipath.com/t/how-to-save-perticular-file-from-outlook-when-the-attachments-contains-multiple-files/921/3

Let me know how it went

Hi mican,
I have already implemented this solution inside my workflow and it works fine!
Means that the file is saved in my folder correctly.
The problem happens when I use HTTP Request and I try to upload the file with a Post method in the company external system. The file is uploaded but is CORRUPTED.
For an unknown reason some rows are inserted on top and at the bottom of the file like previously posted.
The proof is if I remove these rows the file is opened perfectly.
Another strange thing is that if I try to post the same file using POSTMAN I can do it perfectly and the file once posted is downloadable without errors.
I setted in HTTP request the same parameters of POSTMAN, but doesn’t work in the same way!!.
Any idea?

I haven’t tested yet, but you might want to use the MimeKit library

Dim oRequest As System.Net.HttpWebRequest = CType(HttpWebRequest.Create("https://samplewebservice.com"), HttpWebRequest)
Dim msg As MimeEntity
Dim mime_Ct As MimeKit.ContentType
mime_ct= MimeKit.ContentType.Parse(oRequest.ContentType)
Dim mime_part As MimeKit.MimeEntity
mime_part= MimeEntity.Load(mime_ct, oRequest.GetRequestStream())
Dim muli_part As MimeKit.Multipart
muli_part=DirectCast(mime_part,Multipart)
Dim i As Integer
For Each attachment As MimePart In muli_part
    Dim filename As String
    filename = "attachment"+CType(i,String)+".txt"
    Console.WriteLine(filename)
    Dim stream As New FileStream(filename,FileMode.Create)
    attachment.ContentObject.DecodeTo(stream)
    i=i+1
Next

Thanks, You are telling me that I must to install the Mimekit package.
OK. I can do it, but I have no idea about how to invoke your code. Plz, can you post an example that explain how to run the code inside a workflow?

you must using Invoke code activity

Just some doubts:

  1. Invoking this code I must to “exclude” automatically the HTTP request activity?
  2. I suppose that red words are customizable parts. In my case the attachment name is a variable, not the extension.
  3. Are there some other variables to declare?

Thanks

Ad1. Yes
Ad2. Yes, The red text is example
Ad3. Can you tell me what service it is (REST, SOAP)?
Maybe you will need authorization - login or password, certificate etc.
Describe the attachments (extension) and sample answer

Hi Mican,
The service is a REST.
The authorization is a simple http (username + password). No certificate.
The url is similar to “https://xxxxxaaaaaa.appiancloud.com/suite/webapi/0XD1_g”.
The attachment is a .msg file (message file format of MS.Outlook).

I hope this is enough

Again thanks