Can I create tracker ticket with attachments?

Hello,
If there is no attached file, a Tracker Ticket is created normally.
However, if I try to include the attachment, I still get the datatype_mismatch error.
This error is occurs when a request is made to multipart/form-data.
Please help me how to use related_ticket_ids with attachments.
I attached my test code.

[Errors]

{"description":"Validation failed","errors":[{"field":"related_ticket_ids","message":"Value set is of type String.It should be a/an Array","code":"datatype_mismatch"}]}
{"description":"Validation failed","errors":[{"field":"related_ticket_ids","message":"It should contain elements of type Positive Integer only","code":"datatype_mismatch"}]}

============
java test

private Object request(String api, HttpMethod method, HttpEntity body) throws IOException, URISyntaxException {
	URL targetUrl = new URL(config.getFreshdeskEndPoint() + api); // URL object from API endpoint:
	return RestApiUtil.request(targetUrl.toString(), method, body);
}


public JSONObject createTicket(JSONObject data, List<FreshdeskAttachment> attachments) throws IOException, URISyntaxException {
	HttpEntity entity = null;
	if (attachments == null) {
		entity = new StringEntity(data.toString(), ContentType.APPLICATION_JSON.withCharset(Charset.forName(UTF8)));
	} else {
		MultipartEntityBuilder meb = MultipartEntityBuilder.create();
		meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		for (String key : data.keySet()) {
			if (FreshdeskTicketField.CustomFields.equals(key)) {
				JSONObject custom_f = new JSONObject(data.optString(key));
				for (String key1 : custom_f.keySet()) {
					meb.addTextBody(FreshdeskTicketField.CustomFields + "[" + key1 + "]", custom_f.optString(key1), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
				}
			} else if (FreshdeskTicketField.Tags.equals(key)) {
				JSONArray tagArray = data.getJSONArray(FreshdeskTicketField.Tags);
				//meb.addTextBody(FreshdeskTicketField.Tags, "[]"); //if list is empty
				if (tagArray != null && tagArray.length() > 0) {
					for (int i = 0; i < tagArray.length(); i++) {
						meb.addTextBody(FreshdeskTicketField.Tags + "[]", tagArray.optString(i), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
					}
				}
			} else if (FreshdeskTicketField.RelatedTicketIds.equals(key)) {
				JSONArray idArray = data.getJSONArray(FreshdeskTicketField.RelatedTicketIds);
				if (idArray != null && idArray.length() > 0) {
					for (int i = 0; i < idArray.length(); i++) {
						meb.addTextBody(FreshdeskTicketField.RelatedTicketIds + "[]", idArray.optString(i), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
					}
				}
			} else if (FreshdeskTicketField.CcEmails.equals(key)) {
				JSONArray ccEmailsArray = data.getJSONArray(FreshdeskTicketField.CcEmails);
				if (ccEmailsArray != null && ccEmailsArray.length() > 0) {
					for (int i = 0; i < ccEmailsArray.length(); i++) {
						meb.addTextBody(FreshdeskTicketField.CcEmails + "[]", ccEmailsArray.optString(i), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
					}
				}
			} else {
				meb.addTextBody(key, data.optString(key), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
			}
		}
		addFileEntity(meb, attachments);
		entity = meb.build();
	}
	return (JSONObject) request(String.format("/api/v2/tickets"), HttpMethod.POST, entity);
}

Hi,
Welcome to our community!
We analysed the snippet and found that in the line meb.addTextBody(FreshdeskTicketField.RelatedTicketIds + "[]", idArray.optString(i), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8)); , idArray.optString(i) will return a string and not an Array since idArray is an array of integers.

Hi~
Thank you for reply.
I have already tested both addTextBody and addBinaryBody.
Both failed, and the results are as follows.

addTextBody result : {“description”:“Validation failed”,“errors”:[{“field”:“related_ticket_ids”,“message”:“Value set is of type String.It should be a/an Array”,“code”:“datatype_mismatch”}]}
addBinaryBody result : {“description”:“Validation failed”,“errors”:[{“field”:“related_ticket_ids”,“message”:“It should contain elements of type Positive Integer only”,“code”:“datatype_mismatch”}]}

Can you tell me how to pass an integer array using MultipartEntityBuilder?
Or is there a way to solve this problem using another Java library?
Regards,

JSONArray idArray = data.getJSONArray(FreshdeskTicketField.RelatedTicketIds);
if (idArray != null && idArray.length() > 0) {
	for (int i = 0; i < idArray.length(); i++) {
		byte[] ticketIdBytes = ByteBuffer.allocate(4).putInt(idArray.optInt(i)).array();
		meb.addBinaryBody(FreshdeskTicketField.RelatedTicketIds + "[]", ticketIdBytes);
		meb.addTextBody(FreshdeskTicketField.RelatedTicketIds + "[]", idArray.optString(i), ContentType.MULTIPART_FORM_DATA.withCharset(UTF8));
	}
}

Hi @Myeongho_Lee,

We are not sure how the library works or any other libraries in Java. You can check on their repository or approach the contributors if it’s an open-source library or approach StackOverflow for help.

If the expected data-type is provided for the fields, the API will get succeed.

Thanks.

1 Like