I'm facing a weird issue here, I'm trying to send this request with Talend
And I can't figure out how to use the Talend Rest components (tRest and tRestClient)
For the tRest :
I have a response but not the expected one (unfiltered data instead of a single profil base on the idAgent)
And for the tRestClient I tried this way :
and configured this way :
And I have the same response for tRestClient case
Is there something missing in the components configuration (type, ...) or am I doing this wrong ?
Thanks
after trying multiple solutions, configurations, ... and nothing worked. So I created a routine that did what I needed using the java.net.HttpURLConnection library.
here is the routine :
public class plannetAPI {
private final String boundary;
private static final String LINE = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
/**
* plannetAPI: This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
*
* {Category} User Defined
*
* @param rurl
* @param token
* @param charset
* @throws MalformedURLException
*/
public plannetAPI(String rurl, String token, String charset) throws MalformedURLException {
this.charset = charset;
//HttpURLConnection httpConn;
boundary = UUID.randomUUID().toString();
URL url = new URL(rurl);
try {
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("token", token);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Adds a form field to the request
*
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE);
writer.append("Content-Type: text/plain; charset=" + charset).append(LINE);
writer.append(LINE);
writer.append(value).append(LINE);
writer.flush();
}
/**
* Completes the request and receives response from the server.
*
* @return String as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public String finish() throws IOException {
String response = "";
writer.flush();
writer.append("--" + boundary + "--").append(LINE);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = httpConn.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}
response = result.toString(this.charset);
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
return response;
}
}
And then used it in a custom joblet :
And use the routines like this :
try {
routines.plannetAPI multipart = new routines.plannetAPI(((String)globalMap.get("URL")), ((String)globalMap.get("TOKEN")), "utf-8");
// Add form field
multipart.addFormField("idAgent", ((String)globalMap.get("IDAGENT")));
String response = multipart.finish();
System.out.println(response);
row2.id_agent = response;
} catch (Exception e) {
e.printStackTrace();
}
But I would still like to know if there is a way of just doing this with Talend components and no having to work around with custom routines and joblets, ...
Thanks