Jump to content

Recommended Posts


can I create a request with attachment with rest api?

I saw an example in java code but i cant do it in c# code or type script code and I cant do it with postman

some one can help me?

this is the java code example:


import java.io.File;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;

 * This sample class demonstrates how to create a new Change Order with an attachment.
 * @author valre03
 * @version $Revision: ASPEN.0 $
public class SampleNewResourceWithAttachment
    public SampleNewResourceWithAttachment()

            // REST Web Services URI
            String restURI = "http://localhost:8050/caisd-rest";

            // Access Key obtained during Authentication
            String accessKey = "1174913924";

            // Attachment details
            String serverName = "";        // Enter valid host name
            String fileToAttach = "sampletext.txt";
            String repositoryId = "1002";    // Service Desk default

            // Change Order details
            String requestor = "ServiceDesk";


        // Verify fileToAttach exists and is valid
        File binaryFile = new File(fileToAttach);
        if ( ! binaryFile.exists() || ! binaryFile.canRead() || ! binaryFile.isFile() )
            throw new IllegalArgumentException("Invalid value provided for argument fileToAttach.");

        // Change Order payload
        String chgPayload =
                "<chg>" +
                "<requestor COMMON_NAME=\"" + requestor + "\"/>" +
                "<status REL_ATTR=\"APP\"/>" +
                "<description>Created with attachment using REST sample code</description>" +

        // Change Order service URI with attachment (attmnt) data included
        String chgURI = restURI + "/chg" +
                "?repositoryId=" + repositoryId +
                "&serverName=" + serverName +
                "&mimeType=Text" +
                "&description="    + binaryFile.getName();

        System.out.println("Change Order with Attachment POST URI: " + chgURI);

        System.out.println("Change Order payload: " + chgPayload);

        // POST OPERATION -- Create a new Change Order with attachment

        HttpClient client = new HttpClient();

        // Set timeout (not mandatory)

        PostMethod postChgWithAttmnt = new PostMethod(chgURI);
            HttpMethodParams.USE_EXPECT_CONTINUE, false

        try {
            // Change Order payload
            StringPart payloadPart = new StringPart("chg", chgPayload);

            // File attachment payload
            FilePart binaryPart = new FilePart(binaryFile.getName(), binaryFile);

            Part[] parts = { payloadPart, binaryPart };

            // Add the multiple parts to the POST operation
                    new MultipartRequestEntity(parts, postChgWithAttmnt.getParams()));

            // Add corresponding headers
            postChgWithAttmnt.addRequestHeader("Content-Type", "multipart/form-data");
            postChgWithAttmnt.addRequestHeader("X-AccessKey", accessKey);

            System.out.println("Execute POST request for " + chgURI);
            // Execute POST request
            int result = client.executeMethod(postChgWithAttmnt);

            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");

        } catch (HttpException e) {
        } catch (IOException e) {
        } finally {

    public static void main(String[] args)
        new SampleNewResourceWithAttachment();

Link to post
Share on other sites

Hey Vladimir!

I'm not the strongest at C these days (I've been using Python for my API calls), but I would probably approach it by using a call to create a request, then using the returned data to grab the request # to make a second call to create the attachment.

  1. createRequest or createTicket (depending on your use-case)
  2. createAttachment


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...