How to read the content of the file returned by sharepoint Rest API

Copper Contributor

Hello Everyone,

I am using Rest API provided by the SharePoint to read the content of PDF file. 
url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value
method: GET
headers:
Authorization: "Bearer " + accessToken

Response Header:

content-disposition attachment
content-type application/octet-stream


I want to read the file content and send it as an e-mail attachment. I can send the attachment. File name and format is correct but when I preview the attachment, its blank. Could you please help on how I can read so that it get attached in correctly.
Here is how I read the content:

 

byte[] responseResult;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(endpointurl));
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + appReg_bearerToken);
request.Method = "GET";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using(Stream responseStream = response.GetResponseStream())
{
using(MemoryStream memoryStream=new MemoryStream())
{
int count = 0;
byte[] buffer = new byte[4096];
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
responseResult = memoryStream.ToArray();

result = Encoding.UTF8.GetString(responseResult);
}
}
}

return result;

reportCardContent is the 'result' returned from the above.

attachment.Attributes.Add("body", Convert.ToBase64String(new UTF8Encoding().GetBytes(reportCardContent)));
 
I am trying to do this in custom workflow (workflow assembly) in Dynamics 365.

Response from SharePoint : %PDF-1.5
%����
1 0 obj
<</Type/Catalog/Pages 2 0 R/Lang(en-CA) /StructTreeRoot 10 0 R/MarkInfo<</Marked true>>>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[ 3 0 R] >>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R>>/ExtGState<</GS7 7 0 R/GS8 8 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
endobj
4 0 obj
<</Filter/FlateDecode/Length 994>>
stream
x���Mk�0����I�$ B�hh!�҅J�69����Ce�f�]�%o6�ǫ��#����FZ� ��TN�N�N���~�ͷ+�m��gqss�|��A��[q�p/�J�vms�A �e4ܽ��*~k�tn�
���O�{����{�(�:�5���������_���K�u�C�>��c ��mΔ���z�d���T�G<>O����0�)�2=_:=��̙���=%zQ��B���(+�����A:]S-3W�̙�z�H�+�un�N�B}�:�I�KDz����Ȉ��4~c��J���������Reδ��j重�:;��1�[¦BLn�c~���F(���Q��U����z<��Ց:v鬔�-F.�����b����M����� �5�ul���;OM�*�`��."�,���j8�}MK��Y�}6��@YY#(k�a�`_h4����;5� f��P��v�Ä�[}�����Z���8͔\�=x�շ@��t��K�y��t!WKN�Y�!��4D���&
�WW��b�Z�t/�i>�OUdL��`�N`v&��4�� �} Bb� ����;A���>'��� $���#ӏ��X�fDgmRAX��g�&mi�A��4���nY^mr����8��Z�sH�>�]�s�y�絚�����5�k5��z��D��@�Z�


 

 

1 Reply

Have you ever managed to convert this string you get from the API to a .pdf file again? Struggling with the same issue here, but then in Python