setBinaryData

Posted by ssf ssf on 14-Oct-2016 03:11

hi ,

need to attach a file on Rollbase Record with a rest call , but the URL dont supprt a long value. 

how can we do it ?

All Replies

Posted by Anvi Jain on 18-Oct-2016 08:33

Clients like Postman or Insomnia etc cut down the URL due to which the base 64 value gets restricted and hence the file gets corrupted. I have created a work around using HTTPClient in java.The following source code should solve the problem.

The following external jars would also be required-

1.org.apache.commons.httpclient

2.apache-commons-codec

public class ClientPost {

static String url;

static String sessionID = "";

public static void getSession(String loginUrl) throws HttpException, IOException {

HttpClient httpclient = new HttpClient();

PostMethod post = null;

String postResponse = null;

try {

post = new PostMethod(loginUrl);

httpclient.executeMethod(post);

postResponse = post.getResponseBodyAsString();

Pattern pattern = Pattern.compile("<sessionId>(.+?)</sessionId>");

Matcher matcher;

matcher = pattern.matcher(postResponse);

if (matcher.find())

   sessionID = matcher.group(1);

} finally {

post.releaseConnection();

}}

public static void main(String[] args) throws HttpException, IOException {

getSession("LOGINURL");

HttpClient httpclient = new HttpClient();

PostMethod post = null;

String postResponse = null;

try {

if (sessionID != "" || sessionID.length() > 0)

    url = "url" + "?sessionId=" + sessionID;

else

    url = "url";

post = new PostMethod(url);

post.addRequestHeader("loginName", "LOGIN NAME");

post.addParameter("contentType", "CONTENT TYPE");

post.addParameter("fieldName", "FIELD NAME");

post.addParameter("id", "ID");

post.addParameter("fileName","FILE NAME");

post.addParameter("output", "OUTPUT");

String encoding=readFile("FILEPATH");

post.addParameter("value", encoding);

httpclient.executeMethod(post);

postResponse = post.getResponseBodyAsString();

System.out.println(postResponse);

} catch (Exception e) {

e.printStackTrace();

}

post.releaseConnection();

}

public static String readFile(String filePath) throws IOException, InterruptedException {

byte[] bytes = null;

File originalFile = new File(filePath);

try {

FileInputStream fileInputStreamReader = new FileInputStream(originalFile);

bytes = new byte[(int) originalFile.length()];

fileInputStreamReader.read(bytes);

} catch (Exception e) {

e.printStackTrace();

}

String encodeBase64 = Base64.getEncoder().encodeToString(bytes);

return encodeBase64;

}

}

This thread is closed