Jump to content

Recommended Posts

Hi,

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()
    {
        //--- CONFIGURABLE VARIABLES

            // 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";

        //--- END OF CONFIGURABLE VARIABLES


        // 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>" +
                "</chg>";

        // 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)
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

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

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

            // File attachment payload
            FilePart binaryPart = new FilePart(binaryFile.getName(), binaryFile);
            binaryPart.setContentType("application/octet-stream");

            Part[] parts = { payloadPart, binaryPart };

            // Add the multiple parts to the POST operation
            postChgWithAttmnt.setRequestEntity(
                    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: ");
            System.out.println(postChgWithAttmnt.getResponseBodyAsString());
            System.out.println();

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postChgWithAttmnt.releaseConnection();
        }
    }


    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

https://techdocs.broadcom.com/content/broadcom/techdocs/us/en/ca-enterprise-software/business-management/ca-service-management/14-1/reference/ca-service-desk-manager-reference-commands/technical-reference/web-services-methods.html

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.

Guest
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...