Upload Files as Base 64 encoded Strings to API

Post Reply
User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Upload Files as Base 64 encoded Strings to API

Post by bkromer » Tue Jul 07, 2020 2:41 pm

Hi there,
I need to upload files to the Parashift API.

In the docs( https://docs.parashift.io/extraction-ap ... ument.html ) I read that it's possible to send the file as Base64 encoded arrays.
... As an alternative to a list of URLs, files can be delivered as an array of base64 encoded strings...
.

I have no clue how to make this happen... Can I read the File in with Base64 encoding? Like this: "File.read( job.getPath(), "Base64" );"

With the Code below I am getting Status Code 400... bad requests. I guess my code is complete garbage. :|

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var pdf =  File.read( job.getPath(), "Base64" );
	
	var url = "https://extraction.api.parashift.io/v1/666/documents/"; 
	var token = "asdasdadsadsadsadsadsadsadsadsasdasdsasdasdasdasdasdasdad";

	var filename = job.getName();
	
	var theHTTP = new HTTP( HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth;
	theHTTP.authorization = "Bearer "+token;	
	theHTTP.url = url;
	var Body = pdf;
	var postBody = new ByteArray(Body);
	theHTTP.setPostData( postBody,"application/vnd.api+json" );
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	  {
	       job.log( 1, "API CALL IN PROGRESS!" );
	  } 
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
	  {	
		job.log(1,"API CALL successfull Code: "+theHTTP.statusCode +", RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
	  }
	else
	 {
		job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
	 }
}
Can somebody help? :roll: Padawan? :mrgreen:
Benjamin Kromer

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Upload Files as Base 64 encoded Strings to API

Post by bkromer » Tue Jul 07, 2020 2:55 pm

I also looked to the Enfocus docs. ( https://www.enfocus.com/manuals/Develop ... class.html )
There is this convertTo() method for the ByteArray() function.

But still doesnt work... Status Code 400

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var pdf =  File.read( job.getPath(), "utf-8" );
	var url = "https://extraction.api.parashift.io/v1/666/documents/"; 
	var token = "asdadadadasdcasdadsadasd";

	var filename = job.getName();
	
	var theHTTP = new HTTP( HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth;
	theHTTP.authorization = "Bearer "+token;	
	theHTTP.url = url;
	var postBody = new ByteArray( pdf ).convertTo('Base64');  
	theHTTP.setPostData( postBody,"application/vnd.api+json" );
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	  {
	       job.log( 1, "API CALL IN PROGRESS!" );
	  } 
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
	  {	
		job.log(1,"API CALL successfull Code: "+theHTTP.statusCode +", RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
	  }
	else
	 {
		job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
	 }
}
Benjamin Kromer

freddyp
Advanced member
Posts: 576
Joined: Thu Feb 09, 2012 3:53 pm

Re: Upload Files as Base 64 encoded Strings to API

Post by freddyp » Tue Jul 07, 2020 4:02 pm

Code: Select all

var pdf =  File.read( job.getPath(), "utf-8" );
reads the PDF as a string. Afterwards you convert it to a ByteArray, but then the damage is already done. You can read the file directly as a ByteArray:

Code: Select all

var pdf =  File.readByteArray( job.getPath());

User avatar
bkromer
Member
Posts: 72
Joined: Thu Jul 11, 2019 10:41 am
Location: Germany

Re: Upload Files as Base 64 encoded Strings to API

Post by bkromer » Wed Jul 08, 2020 10:12 am

Thanks freddyp that makes sense.
Tried it, I also read that I need to post a JSON with the Base64 file in it.

The JSON should look like this:

Code: Select all

{
  "data": {
    "type": "documents",
    "attributes": {
      "urls": ["https://www.domain.com/download/invoice-1234.pdf"],

      // as an alternative to urls:
      "base64_files": ["JVBERi0xLjQNCiWhs8XXDQoxIDAgb2JqDQo8PC9Db3VudC..."]

      "custom_fields": { "customer_id": "1234" },

      "document_type": "receipt",
      "extract_line_items": true
    }
  }
}
I tried this, but still getting status code 400: :cry:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	
	var theHTTP = new HTTP( HTTP.SSL );
	theHTTP.authScheme = HTTP.OauthAuth;
	theHTTP.authorization = "Bearer asasdasdasdasd";	
	theHTTP.url ="https://extraction.api.parashift.io/v1/666/documents/"; 
	var postBody = new ByteArray( '{\"data\": {\"type\": \"documents\",\"attributes\": {\"urls\": [],\"base64_files\":'+ File.readByteArray( job.getPath() ) +',\"custom_fields\": {  },\"document_type\": \"receipt\",\"extract_line_items\": true}} } ') ;  
	theHTTP.setPostData( postBody,"application/vnd.api+json" );
	theHTTP.post();
	
	while( !theHTTP.waitForFinished( 3 ) )
	  {
	       job.log( 1, "API CALL IN PROGRESS!" );
	  } 
	if( theHTTP.finishedStatus == HTTP.Ok && theHTTP.statusCode == 200 )
	  {	
		job.log(1,"API CALL successfull Code: "+theHTTP.statusCode +", RESPONSE: "+theHTTP.getServerResponse().toString( "UTF-8" ));
	  }
	else
	 {
		job.log(3,"API CALL Failed:  "+theHTTP.getServerResponse().toString( "UTF-8" )+ "  Status Code: "+theHTTP.statusCode.toString( "UTF-8" ));
	 }
}
Benjamin Kromer

Post Reply