Upload Files as Base 64 encoded Strings to API

Post Reply
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Upload Files as Base 64 encoded Strings to API

Post by bkromer »

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
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: Upload Files as Base 64 encoded Strings to API

Post by bkromer »

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
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Upload Files as Base 64 encoded Strings to API

Post by freddyp »

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());
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: Upload Files as Base 64 encoded Strings to API

Post by bkromer »

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
Post Reply