<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question How to convert text file to ByteArray and send it via API in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-to-convert-text-file-to-ByteArray-and-send-it-via-API/m-p/372405#M241245</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;kindly need your support in below,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have Text file, I have to convert it to byteArray and send it through REST API, I tried many ways to convert the file to byteArray, last one is:&lt;BR /&gt;- ExecuteScript with below code:&lt;BR /&gt;import base64&lt;BR /&gt;import codecs&lt;/P&gt;&lt;P&gt;with codecs.open('/path/to/input/file.txt', 'r', encoding='utf-8') as file:&lt;BR /&gt;file_content = file.read()&lt;BR /&gt;file_bytes = file_content.encode('utf-8')&lt;BR /&gt;encoded_bytes = base64.b64encode(file_bytes)&lt;BR /&gt;encoded_string = encoded_bytes.decode('utf-8')&lt;/P&gt;&lt;P&gt;flowFile = session.putAttribute(flowFile, 'fileContent', encoded_string)&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but still get error: utf-8' codec can't decode byte 0xff in position 0: unexpected code byte in &amp;lt;script&amp;gt; at line number 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REST API Config:&lt;BR /&gt;HTTP Method: POST&lt;BR /&gt;API URL:.....&lt;BR /&gt;Request Content-Type: application/octet-stream&lt;/P&gt;&lt;P&gt;authentication User: username&lt;/P&gt;&lt;P&gt;&amp;nbsp;authentication password: password&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;we applied it in VB.net as below:&lt;BR /&gt;Function CallApi(APIURL As String,filepath As String) As String&lt;BR /&gt;&lt;BR /&gt;Dim req As WebRequest = WebRequest.Create(APIURL)&lt;BR /&gt;Dim txtfile = File.ReadAllBytes(filepath"file.txt")&lt;BR /&gt;System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12&lt;BR /&gt;ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications&lt;BR /&gt;req.Headers("Authorization") = "Basic " &amp;amp; Convert.ToBase64String(Encoding.[Default].GetBytes("username:password"))&lt;BR /&gt;req.Method = "POST"&lt;BR /&gt;req.ContentType = "application/octet-stream"&lt;BR /&gt;Dim byteArray As Byte() = txtfile&lt;BR /&gt;req.ContentLength = byteArray.Length&lt;BR /&gt;Dim dataStream As Stream = req.GetRequestStream()&lt;BR /&gt;dataStream.Write(byteArray, 0, byteArray.Length)&lt;BR /&gt;dataStream.Close()&lt;BR /&gt;' response&lt;BR /&gt;Dim resp As WebResponse = req.GetResponse()&lt;BR /&gt;Dim s As Stream = resp.GetResponseStream()&lt;BR /&gt;Dim sr As StreamReader = New StreamReader(s, Encoding.UTF8)&lt;BR /&gt;Dim doc As String = sr.ReadToEnd()&lt;BR /&gt;Return doc&lt;BR /&gt;End Function&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Any suggestions may help me to achieve this?&lt;/P&gt;</description>
    <pubDate>Sat, 10 Jun 2023 00:53:36 GMT</pubDate>
    <dc:creator>MukaAddA</dc:creator>
    <dc:date>2023-06-10T00:53:36Z</dc:date>
    <item>
      <title>How to convert text file to ByteArray and send it via API</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-convert-text-file-to-ByteArray-and-send-it-via-API/m-p/372405#M241245</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;kindly need your support in below,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have Text file, I have to convert it to byteArray and send it through REST API, I tried many ways to convert the file to byteArray, last one is:&lt;BR /&gt;- ExecuteScript with below code:&lt;BR /&gt;import base64&lt;BR /&gt;import codecs&lt;/P&gt;&lt;P&gt;with codecs.open('/path/to/input/file.txt', 'r', encoding='utf-8') as file:&lt;BR /&gt;file_content = file.read()&lt;BR /&gt;file_bytes = file_content.encode('utf-8')&lt;BR /&gt;encoded_bytes = base64.b64encode(file_bytes)&lt;BR /&gt;encoded_string = encoded_bytes.decode('utf-8')&lt;/P&gt;&lt;P&gt;flowFile = session.putAttribute(flowFile, 'fileContent', encoded_string)&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but still get error: utf-8' codec can't decode byte 0xff in position 0: unexpected code byte in &amp;lt;script&amp;gt; at line number 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REST API Config:&lt;BR /&gt;HTTP Method: POST&lt;BR /&gt;API URL:.....&lt;BR /&gt;Request Content-Type: application/octet-stream&lt;/P&gt;&lt;P&gt;authentication User: username&lt;/P&gt;&lt;P&gt;&amp;nbsp;authentication password: password&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;we applied it in VB.net as below:&lt;BR /&gt;Function CallApi(APIURL As String,filepath As String) As String&lt;BR /&gt;&lt;BR /&gt;Dim req As WebRequest = WebRequest.Create(APIURL)&lt;BR /&gt;Dim txtfile = File.ReadAllBytes(filepath"file.txt")&lt;BR /&gt;System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12&lt;BR /&gt;ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications&lt;BR /&gt;req.Headers("Authorization") = "Basic " &amp;amp; Convert.ToBase64String(Encoding.[Default].GetBytes("username:password"))&lt;BR /&gt;req.Method = "POST"&lt;BR /&gt;req.ContentType = "application/octet-stream"&lt;BR /&gt;Dim byteArray As Byte() = txtfile&lt;BR /&gt;req.ContentLength = byteArray.Length&lt;BR /&gt;Dim dataStream As Stream = req.GetRequestStream()&lt;BR /&gt;dataStream.Write(byteArray, 0, byteArray.Length)&lt;BR /&gt;dataStream.Close()&lt;BR /&gt;' response&lt;BR /&gt;Dim resp As WebResponse = req.GetResponse()&lt;BR /&gt;Dim s As Stream = resp.GetResponseStream()&lt;BR /&gt;Dim sr As StreamReader = New StreamReader(s, Encoding.UTF8)&lt;BR /&gt;Dim doc As String = sr.ReadToEnd()&lt;BR /&gt;Return doc&lt;BR /&gt;End Function&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Any suggestions may help me to achieve this?&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 00:53:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-convert-text-file-to-ByteArray-and-send-it-via-API/m-p/372405#M241245</guid>
      <dc:creator>MukaAddA</dc:creator>
      <dc:date>2023-06-10T00:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert text file to ByteArray and send it via API</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-convert-text-file-to-ByteArray-and-send-it-via-API/m-p/372502#M241250</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/105480"&gt;@MukaAddA&lt;/a&gt;,&amp;nbsp;Welcome to our community! To help you get the best possible answer, I have tagged in our NiFi experts &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/103151"&gt;@cotopaul&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/95503"&gt;@steven-matison&lt;/a&gt;&amp;nbsp;who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please feel free to provide any additional information or details about your query, and we hope that you will find a satisfactory solution to your question.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 08:34:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-convert-text-file-to-ByteArray-and-send-it-via-API/m-p/372502#M241250</guid>
      <dc:creator>VidyaSargur</dc:creator>
      <dc:date>2023-06-12T08:34:16Z</dc:date>
    </item>
  </channel>
</rss>

