Question

1
Replies
2935
Views
allac Member since 2017 9 posts
PEGA
Posted: 2 years ago
Last activity: 2 years 7 months ago
Closed

Need help in implementing Connect-REST (Post Method for sending Document)

We have requirement to implement Connect-Rest to post documents over the web service for 3rd party application, and 3rd party Rest service implemented in such away that we have to send 3 parameters in request body itself as a multipart.

we have implemented solution using java code and its working fine as expected when we run through eclipse, but we are facing issues when we try to implement same java code in activity java step. we are expecting assistance on implementing this with either Connect-REST or using JAVA step in actvity.

Note:

Instructions on https://pdn.pega.com/support-articles/unable-send-attachment-connect-rest have already been used. But there are 2 additional parameters called SessionTicket and folderUid which have to be sent as Strings. But in InvokeHTTPConnector, everything is getting converted into Bytes and hence the webservice is failing.

Java Code which implemented in eclipse:

----------------------------------------------------------------------------------------------

import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class CallProj {
public CallProj() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
uploadDocumentToProjector("C:\\xxxxxx\\eclipse-workspace\\ProjectorRestService\\src\\Resume_Doc.doc\\");
}
public static void uploadDocumentToProjector(String fileName) {
// the file we want to upload
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// server back-end URL
HttpPost httppost = new HttpPost("https://doc.projectorpsa.com/2/AjxAddDocument");
FileBody bin = new FileBody(new File(fileName));
HttpEntity entity = (HttpEntity) MultipartEntityBuilder.create()
.addPart("SessionTicket", new StringBody("xxxxxxxxxxxx", ContentType.TEXT_PLAIN))
.addPart("folderUid", new StringBody("xxxxxxx", ContentType.TEXT_PLAIN))
.addPart("file", bin).build();
httppost.setEntity(entity);
// execute the request
CloseableHttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
System.out.println("[" + statusCode + "] " + responseString);
} catch (Exception e) {
System.err.println("Exception");
e.printStackTrace();
}
}
}

----------------------------------------------------------------------------------------

java code which we tried in java step in Activity:

----------------------------------------------------------------------------------------------------------------------------------

String fileName= "XXXXXXXXXXX";
String SessionTicket= "ApTmcqHc4YkYDhq2YdQYyw==";
String folderUid = "xxxxxxxxxxxxx";
com.pega.apache.http.impl.client.DefaultHttpClient client = null;
com.pega.apache.http.HttpResponse httpResponse = null;
try {
if (client == null)
{
client = new com.pega.apache.http.impl.client.DefaultHttpClient();
}
// server back-end URL
com.pega.apache.http.client.methods.HttpPost httppost = new com.pega.apache.http.client.methods.HttpPost("https://doc.projectorpsa.com/2/AjxAddDocument");
//Create file body
com.pega.apache.http.entity.mime.content.FileBody bin = new com.pega.apache.http.entity.mime.content.FileBody(new java.io.File(fileName));
com.pega.apache.http.HttpEntity entity = new com.pega.apache.http.entity.mime.MultipartEntity();
((com.pega.apache.http.entity.mime.MultipartEntity) entity).addPart("SessionTicket", new com.pega.apache.http.entity.mime.content.StringBody(SessionTicket));
((com.pega.apache.http.entity.mime.MultipartEntity) entity).addPart("folderUid", new com.pega.apache.http.entity.mime.content.StringBody(folderUid));
((com.pega.apache.http.entity.mime.MultipartEntity) entity).addPart("file", bin);
httppost.setEntity(entity);
httpResponse = client.execute(httppost, (com.pega.apache.http.protocol.BasicHttpContext)localContext); // This step not excuting,and moving to catch block
} catch (Exception e) {
System.err.println("Exception couured");
}
-------------------------------------------------------------------------------------------------
Please suggest your thoughts to implement our requirement.
Thank you.
Data Integration
Moderation Team has archived post
Share this page LinkedIn