fresh-samples
fresh-samples copied to clipboard
Notes with attachment not working
I use below code for uploading a note with attachments. But I received following error in response.
errorcode 400 - {"description":"Validation failed","errors":[{"field":"body","message":"It should be a/an String","code":"missing_field"}]}
If i comment out following line, able to upload body alone.
meb.addBinaryBody("attachments[]", attach2, ContentType.TEXT_PLAIN.withCharset("utf-8"), attach2.getName());
` public static String getArrayForUrlPostv2(String urlString) {
try {
final HttpClientBuilder hcBuilder = HttpClientBuilder.create();
final RequestBuilder reqBuilder = RequestBuilder.post();
final RequestConfig.Builder rcBuilder = RequestConfig.custom();
// URL object from API endpoint:
URL url = new URL(urlString);
final String urlHost = url.getHost();
final int urlPort = url.getPort();
final String urlProtocol = url.getProtocol();
reqBuilder.setUri(url.toURI());
// Authentication:
List<String> authPrefs = new ArrayList<>();
authPrefs.add(AuthSchemes.BASIC);
rcBuilder.setTargetPreferredAuthSchemes(authPrefs);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(urlHost, urlPort, AuthScope.ANY_REALM),
new UsernamePasswordCredentials(PreferenceMgr.getString(PreferenceMgr.KEY_AGENT_EMAIL),
PreferenceMgr.getString(PreferenceMgr.KEY_AGENT_PASSWORD)));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
AuthCache authCache = new BasicAuthCache();
AuthSchemeBase authScheme = new BasicScheme();
authCache.put(new HttpHost(urlHost, urlPort, urlProtocol), authScheme);
HttpClientContext hccContext = HttpClientContext.create();
hccContext.setAuthCache(authCache);
// Body:
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
File attach2 = ApplicationNekt.getContext().getFileStreamPath(Constants.LogFileName + ".txt");
meb.addBinaryBody("attachments[]", attach2,
ContentType.TEXT_PLAIN.withCharset("utf-8"), attach2.getName());
//meb.addTextBody("body", "testing");
meb.addTextBody("body", "test", ContentType.TEXT_PLAIN);
meb.addTextBody("private", "true", ContentType.TEXT_PLAIN);
reqBuilder.setEntity(meb.build());
// Execute:
RequestConfig rc = rcBuilder.build();
reqBuilder.setConfig(rc);
HttpClient hc = hcBuilder.build();
HttpUriRequest req = reqBuilder.build();
HttpResponse response = hc.execute(req, hccContext);
// Print out:
HttpEntity body = response.getEntity();
InputStream is = body.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("utf-8")));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
int response_status = response.getStatusLine().getStatusCode();
String response_body = sb.toString();
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Response Status: " + response_status);
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Body:\n");
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, response_body);
if (response_status > 400) {
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "X-Request-Id: " + response.getFirstHeader("x-request-id").getValue());
} else if (response_status == 201) {
//For creation response_status is 201 where are as for other actions it is 200
try {
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Update Successfull");
//Creating JSONObject for the response string
JSONObject response_json = new JSONObject(sb.toString());
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Ticket ID: " + response_json.get("id"));
return "success";
//Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Location : " + response.getFirstHeader("location").getValue());
} catch (JSONException e) {
Utilities.writeToLogFile(Constants.LOG_ERROR_LEVEL, "Error in JSON Parsing\n :" + e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
`