kinaesthesia kinaesthesia - 5 months ago 25
reST (reStructuredText) Question

Creation API of an external service. Sending binary data

The external service is not mine, it's "Freshdesk".

I managed to create a Java-API with Java-ws-rs to be able to call its WS to create a "Ticket".

Here is my Interface:

public interface FreshdeskService {

TicketResponse create(TicketRequest ticketRequests);


And here my object (I removed a lot of field cause it's not necessary here):

public class TicketRequest {
private String name;
private Long requester_id;
private String email;

Everything is fine and the ticket can be created. My issue is when I tried to add an attachment to this ticket. Here is the documentation:

The curl command is:

curl -v -u -F "attachments[]=@/path/to/attachment1.ext" -F "attachments[]=@/path/to/attachment2.ext" -F "" -F "subject=Ticket Title" -F "description=this is a sample ticket" -X POST ''

As you can see, they turn the data into a form with
. But I don't know how to manage to do that with my API. I can add an
in my
object but it doesn't work. Any help?


Disclaimer: It's part of the answer. Waiting for further clarification to add more details.

The Freshdesk documentation states the following regarding creating a ticket with attachments:

This API request must have its content-type set to multipart/form-data.

Hence, you won't be able to create a ticket with attachments using JSON.

Depending on the JAX-RS implementation you are using, there are different ways to send/receive files.

If you are using Jersey 2.x, you need to add the multipart dependency and register the multipart feature in order to handle multipart requests. Check this answer for more details on how to do that. You can use FormDataMultiPart to upload the file from your client.