Search code examples
wsdlbase64servicenowpowershell-5.0blueprism

Upload Attachment to ServiceNow using SOAP and Base64 via BluePrism


I am trying to upload a file into ServiceNow using the WSDL <https://instanceName.Service-now.com/ecc_queue.do?WSDL>

I am converting the file to Base64 using Powershell. The upload via SOAPUI works fine, however the same file when downloaded is corrupted. If I upload an Excel file with data on downloading the file the Excel file is Empty

Source File Name is tp-certification-guide.pdf (Source PDF: - https://www.servicenow.com/content/dam/servicenow/other-documents/training/tp-certification-guide.pdf)

However, when I convert the file to Base64 using a portal (https://www.browserling.com/tools/file-to-base64/), I am able to upload and download the file without any issues. Again the file uploaded to ServiceNow does not get corrupted nor does it download an Empty File.

My SoapUI code: -

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ecc="http://www.service-now.com/ecc_queue">
<soapenv:Header/>
  <soapenv:Body>
    <ecc:insert>
      <agent>AttachmentUploader</agent>
      <name>problem_data.pdf:application/pdf</name>
      <payload>AAAAIGZ0eXBxdCrG[..truncated..]</payload>;
      <source>incident:[sysid is here]</source>
      <topic>AttachmentUploader</topic>
    </ecc:insert>
  </soapenv:Body>
</soapenv:Envelope>
Above Code taken from https://www.servicenowguru.com/integration/sending-attachment-servicenow/

My PowerShell code is as below

$InputFile = "D:\02_Downloads\ChromeDLs\tp-certification-guide.pdf"

# Read the file as text
$Text = [System.IO.File]::ReadAllText($InputFile)

# Convert the string to UTF-8 bytes
$UTF8Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)

# Encode the UTF-8 bytes as a Base64 string
$Base64String = [System.Convert]::ToBase64String($UTF8Bytes)

$Base64String | Out-File "D:\02_Downloads\ChromeDLs\tp-certification-guide.txt"

The file should uploaded to ServiceNow using the WSDL and the same file when downloaded should not be corrupted or empty.

I feel there is something wrong in the generation of Base64 using Powershell compared to the Online Edition which would need assistance.


Solution

  • I can't help you with the base64 problem, but if you're interested, then here's my dotnet code to upload attachment to SNOW using REST.

    Dim request As WebRequest = WebRequest.Create(REST_Address)
    request.UseDefaultCredentials = True
    request.Credentials = new NetworkCredential(SNOW_Username, SNOW_Password)
    request.Method = "POST"
    request.ContentType = ContentType
    
    
    Dim byteArray As Byte() = System.IO.File.ReadAllBytes(Filename)
    
    request.ContentLength = byteArray.Length
    Dim RequestStream As System.IO.Stream = request.GetRequestStream()
    RequestStream.Write(byteArray, 0, byteArray.Length)
    RequestStream.close()
    
    Dim response As WebResponse = request.GetResponse
    Dim responseStream As IO.Stream = response.GetResponseStream
    Dim sr As New IO.StreamReader(responseStream)
    
    resultData = sr.ReadToEnd
    response.Close
    

    Inputs

    REST address example: https://instance.service-now.com/api/now/attachment/file?table_name=SNOWTable&table_sys_id=32charsysid&file_name=test.pdf

    content type: application/pdf