News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Sending a fax attachment using PowerShell
Tags: rest api
Nov 25, 2019 at 2:35pm   •   7 replies  •  0 likes
Manny Baylor

Hi, I've been accessing your API via PowerShell and everything has gone pretty smooth up to this point. However, now I'm trying to write a script that is able to send a fax with an attachment and I'm having issues with submitting the fax job if it has an attachment. The only type of attachment I have been able to send up to this point is plain text document, but I'd like to be able to send a PDF.

I'm currently sending using the multipart/mixed format. I tried to mimic the format that is in your API Reference page for createFaxMessage where the content-type is set as "multipart/form-data" and transmit the file in base64 encoded format, but at best I get the base64 data as plain text after the cover page.

Here is a copy of the message body as I'm sending it over. I've also set the content type as application/pdf which does not change the results: https://pastebin.com/phB61jy4

If the Base64 text is very small then the fax will deliver with the second page containing the base64 in plain text format. If the Base64 isn't very small (like the sample pdf in my pastebin which is 26kb), then I won't receive the fax at all. If I run a MessageStore API request a few minutes later, I see the status of the message updated to indicate messageStatus=SendingFailed; faxErrorCode=RenderingFailed

Is there a way to structure the message body so that your API recognizes the file as a PDF and decodes it properly? Since I'm using PowerShell to do this I'm unable to use your pre-packaged API, but up to this point it hasn't been a problem. If only I knew what your traffic API processor was looking for, I could probably shape the traffic so that it would work without issue.

6 Answers
answered on May 23, 2020 at 11:49am  

Were your changes successful, and did the API deliver both a rendered pdf document and return a name of the attached document to your account?

I am having trouble returning a meaningful attachment name, as the name of the faxed attachment takes on the temporary folder identification.

Do you know of a method to post a curl so that a new name can replace the file name generated by the temporary folder? This is shown in the example demonstrated at

https://developers.ringcentral.com/api-reference/Fax/createFaxMessage

url --request POST \
  --url https://platform.devtest.ringcentral.com/restapi/v1.0/account/accountId/extension/extensionId/fax \
  --header 'accept: application/json' \
  --header 'content-type: multipart/form-data' \  --data '{"attachment":"data:application/pdf;name=A%Different%Name.pdf;base64,JVBERi0xLjMK…lRU9GCg==","to":["1234567890"]}'

in which I have defined the new name as

  A%Different%Name.pdf

My attempts have failed. Either I generate a rendered pdf doc, or I generate a new name, but not both.


 0
answered on Dec 4, 2019 at 11:23am  

I was finally able to figure out this issue! Turns out my original code I posted to https://pastebin.com/NFcdDtdR is pretty close, but I needed to make a couple changes in the boundry area where the attachment data is.

1. At line 12 where I previously defined $FileData replace with the following:

# Get File Data
$fileBin = [System.IO.File]::ReadAllBytes($FilePath)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

2. Change line 31, name=""fileData"" (though not sure if that matters)

3. Remove line 32

4. Change line 33 to application/pdf

OR you can detect the content type before populating $RingCentralSendFax and reference $ContentType value as the Content-Type

#Determine Mime Type
Add-Type -AssemblyName System.Web
$mimeType = [System.Web.MimeMapping]::GetMimeMapping($FilePath)
if ($mimeType) {
    $ContentType = $mimeType
} else {
    $ContentType = "application/octet-stream"
}

5. Replace line 35 with the following:

$enc.GetString($fileBin),

That should work. Thanks for your help!


 0
answered on Dec 3, 2019 at 10:52am  

I found the curl generated by the API explorer (posted in my response above) was incorrect. So basically you don't need to convert to based64 string.

Can you follow the curl example in this blog article to change your script code accordingly.


 0
answered on Dec 3, 2019 at 9:53am  

Part 1

# Pack the JSON DATA

$todestination = [pscustomobject]@()
$todestination += @{
    phoneNumber = "+14435551212"
    name = "Test Recipient"
}

### CONVERT FILE CONTENT TO BASE64 STRING
$FilePath = "$($env:TEMP)\TestFax.PDF"
$objFile = Get-ItemProperty -Path $FilePath
$FileData = [Convert]::ToBase64String([IO.File]::ReadAllBytes($FilePath))

# Create Attachment Param
$attachmentdata = "data:application/pdf;name=$((Get-ItemProperty -Path $FilePath).Name);base64,$($FileData)"

$RingCentralSendFaxJson = [pscustomobject]@{}
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name attachment -Value $attachmentdata
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name to -Value $todestination
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name faxResolution -Value "High"
Add-Member -InputObject $RingCentralSendFaxJson -MemberType NoteProperty -Name coverPageText -Value "Have cover page text"



 0
answered on Nov 27, 2019 at 1:15pm  

Phong, so that's what I originally tried to do when I wrote my script, but no matter what I tried the API processor wouldn't accept the message. I keep getting the "Bad Request" error message.

In the CURL API documentation and in the sample you have provided, it appears that the attachment and the PDF base64 is included in the JSON formatting in the message body. I replicated this in my own message and it doesn't accept the fax even when I don't include an attachment.

Here is the code from that test:
https://pastebin.com/fZTH71H1

This is an exact copy of what the Message Body looks like (although admittedly this particular test:
https://pastebin.com/AiDKngf3

I have not had any issue in sending JSON data with the other messaging API's. Not sure why this is an issue, since this data appears to mimic the CURL formatting in the API Documentation.


 0
answered on Nov 27, 2019 at 10:25am  

Thanks for the code reference!

In your question you said setting the content-type as "multipart/form-data" but in the code I found this

"https://platform.devtest.ringcentral.com/restapi/v1.0/account/$($accountId)/extension/$($extensionId)/fax" -Method Post -ContentType "multipart/mixed; boundary=$($boundary)" -Body $RingCentralSendFax -Headers $accesstoken.Header

Not sure what was wrong but can you change the content-type to "multipart/form-data"

Here is a typical curl request to send a fax

curl --request POST \
  --url 'https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/fax' \
  --header 'accept: application/json' \
  --header 'authorization: Bearer xxxxxF8QUE' \
  --header 'content-type: multipart/form-data' \
  --data '{"attachment":"data:application/pdf;name=scan1172.pdf;base64,JVBERi0xLjQ..../......9kKZW5kc3RyZWFVFT0Y=","to":["1650XXXXXXX"],"coverPageText":"Have cover page text"}'



 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us