Question

Byte Array in REST Service response - GET method

I have a REST service which return a byte Array in the response payload. Just the byte Array , nothing else. This byte array is the file sent by the service provider.

How do I map that byte array and get the content and show it on the Pega side ?

1. Mapping the byte array to a temp property, gives me the byte array as a raw string, how do I use that string as a byte array as it is ?

Any other way of mapping the response in the REST response which could help ?

Correct Answer
June 20, 2019 - 12:32am

Yes .. we were able to get that working some how with the below code snippet:

java.net.HttpURLConnection httpUrlConn = (java.net.HttpURLConnection)ssURL.openConnection();

//Set the REST request headers and body on httpURLConn using  httpUrlConn.setRequestProperty(<HeaderName>, <VALUE>);

java.io.InputStream iStream = httpUrlConn.getInputStream();

java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();

byte bByte;

int i=iStream.available();

byte[] bArray=new byte[4096];

 

while((i=iStream.read(bArray))!= -1)

{

buffer.write(bArray,0,i);

}    

 

bArray = buffer.toByteArray();    

httpUrlConn.disconnect();

HashStringMap aMap = new HashStringMap();

aMap.put( "ContentDisposition", "inline;filename="+FileName);

Response = tools.sendFile(bArray, FileName , false, aMap, true );

Comments

Keep up to date on this post and subscribe to comments

May 14, 2019 - 7:49am
Response to KevinZheng_GCS

 

 

Thanks Kevin , but this post talks about sending the byte array out of Pega in post method.

I am looking for my connector to get the response from the service provider, I am consuming the service which send me byte array , which I need to consume and read the file.

 

 

May 14, 2019 - 8:04am

Depends on what the byte array represents. For example, if its a file, take a look at Pega API service pzGetPRConfigFile.

June 18, 2019 - 3:41am

Hi Interested on this topic. KULKARNIA, did you a find any solution in the end?

June 20, 2019 - 12:32am

Yes .. we were able to get that working some how with the below code snippet:

java.net.HttpURLConnection httpUrlConn = (java.net.HttpURLConnection)ssURL.openConnection();

//Set the REST request headers and body on httpURLConn using  httpUrlConn.setRequestProperty(<HeaderName>, <VALUE>);

java.io.InputStream iStream = httpUrlConn.getInputStream();

java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();

byte bByte;

int i=iStream.available();

byte[] bArray=new byte[4096];

 

while((i=iStream.read(bArray))!= -1)

{

buffer.write(bArray,0,i);

}    

 

bArray = buffer.toByteArray();    

httpUrlConn.disconnect();

HashStringMap aMap = new HashStringMap();

aMap.put( "ContentDisposition", "inline;filename="+FileName);

Response = tools.sendFile(bArray, FileName , false, aMap, true );