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"}'
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.
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"
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.
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!
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.