4 Replies Latest reply on Jun 3, 2019 11:25 AM by pascal Auberson

    Incident creation via REST API: adding attachments

    pascal Auberson
      Share This:

      Hi all,

       

      I am in the middle of playing around with the REST interface based on ITSM v9.1 and the Postman-App v7.1.1

       

      Creating simple INC records via the 'HPD:IncidentInterface_Create' form works fine so far - incl. Worklog entry (see example).

       

      {

          "values": {

              "First_Name": "test",

              "Last_Name": "user",

              "Description": "TE REST API: Incident Creation",

              "Detailed_Decription": "here are the notes",

              "HPD_CI_ReconID": "REGAAEOEEO7X8ANT3487F9E6113ESB",

              "Impact": "3000",

              "Urgency": "4000",

              "Reported Source": "Systems Management",

              "Service_Type": "Infrastructure Event",

              "Assigned Support Company": "Comp",

              "Assigned Support Organization": "Engineering",

              "Assigned Group": "ENG-ITSM",

              "z1D_Action": "CREATE",

              "z1D_WorklogDetails": "WorkInfo Summary",

              "z1D_Details": "WorkInfo Notes",

              "z1D_Secure_Log": "No",

              "z1D_View_Access": "Internal",

              "z2AF_Act_Attachment_1/attachmentName": "attachment.txt",

              "z2AF_Act_Attachment_1_attachmentData": "dGVzdCBhdHRhY2htZW50",

              "z2AF_Act_Attachment_1_attachmentOrigSize": ""

          }

      }

       

      Where I a still struggling,  is adding attachments. I read the BMC docu page, but it didn't help me to get a clue how to setup/integrate the Code within Postman. So if anyone may help, your support is appreciated and welcomed.

       

      Regards, Pascal

        • 1. Re: Incident creation via REST API: adding attachments
          Karthick Krishnan

          try using curl. Seems like postman has certain limitation in sending multipart data.

           

          Provided below is link to BMC doc on how to send attachments to REST

           

          Remedy - MidTier - How to send attachments via the Remedy REST API

          1 of 1 people found this helpful
          • 2. Re: Incident creation via REST API: adding attachments
            Sachin Tappe

            Postman does not support a multi-part/form-data content type which is required for including the attachment element.

            In order to create Incident with Attachment, the REST API call consists of two parts

            1. Application Json for the Incident entry element
            2. Attachment file(s).

             

            I am sending you Jersey based Java REST client code snippet that you possibly use

             

            public Response postRequest(String path, String parameterName, JsonObject entryJson,

            HashMap<String, String> attachments) {

            Response response;

            FormDataMultiPart multiPart = new FormDataMultiPart();

            FormDataBodyPart entryPart = new FormDataBodyPart(parameterName, entryJson, MediaType.APPLICATION_JSON_TYPE);

            multiPart.bodyPart(entryPart);

             

            for (Entry<String, String> attachment : attachments.entrySet()) {

            String attachmentFieldName = attachment.getKey();

            String attachmentFilePath = attachment.getValue();

            FormDataBodyPart attachmentPart = new FileDataBodyPart("attach-" + attachmentFieldName,

            new File(attachmentFilePath), MediaType.APPLICATION_OCTET_STREAM_TYPE);

            multiPart.bodyPart(attachmentPart);

            }

            Entity<FormDataMultiPart> entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA);

            response = createRequest(path).post(entity, Response.class);

            return response;

            }

            • 3. Re: Incident creation via REST API: adding attachments
              Sachin Tappe

              Another sample

               

              public static void createIncidentWithAttachment(String pstrAuthorisation) {

                      // start HTTP POST to create an entry

              CloseableHttpClient httpClient = HttpClients.createDefault();

              HttpPost httpPost = new HttpPost(

              "http://mobility123-rest.trybmc.com/api/arsys/v1/entry/HPD:IncidentInterface_Create");

              String filePath = "error.txt";

               

                      // build the JSON entry

              String json = "{ \"values\" : { ";

                      json += "\"First_Name\" : \"Allen\", ";

                      json += "\"Last_Name\" : \"Allbrook\", ";

                      json += "\"Description\" : \"Incident Crated By Rest Client\", ";

               

                      json += "\"Service_Type\" : \"Infrastructure Event\", ";

                      json += "\"Reported Source\" : \"Direct Input\", ";

                      json += "\"Impact\" : \"4-Minor/Localized\", ";

                      json += "\"Urgency\" : \"4-Low\", ";

                      json += "\"Status\" : \"New\", ";

                      json += "\"Status_Reason\" : \"Infrastructure Change Created\", ";

                      json += "\"Direct Contact Company\" : \"Calbro Services\", ";

                      json += "\"Phone_Number\" : \"1234567889\", ";

                      json += "\"Contact_Company\" : \"Calbro Services\", ";

                      json += "\"z1D_Activity_Type\" : \"Working Log\", ";

                      json += "\"z1D_Details\" : \"REST API Test\", ";

                      json += "\"z1D_WorklogDetails\" : \"REST API Test Worklog\", ";

                      json += "\"z2AF_Act_Attachment_1\" : \"error.log\", ";

                      json += "\"HPD_CI\" : \"TestComputer\", ";

                      json += "\"HPD_CI_ReconID\" : \"1234\"";

                      json += " } }";

               

               

                      // build the multipart entity

              HttpEntity entity = MultipartEntityBuilder.create().addTextBody("entry", json, ContentType.APPLICATION_JSON)

              .addBinaryBody("attach-z2AF_Act_Attachment_1", new File(filePath)).build();

                      httpPost.setEntity(entity);

                      httpPost.addHeader("Authorization", pstrAuthorisation);

              HttpResponse response = null;

               

                      try {

                      // make the call and print the Location

                      response = httpClient.execute(httpPost);

                      } catch (Exception e) {

              e.printStackTrace();

                      }

              Header locationHeader = response.getFirstHeader("Location");

              System.out.println(locationHeader.getValue());

                  }

              • 4. Re: Incident creation via REST API: adding attachments
                pascal Auberson

                Thank you for your feedback and ideas!

                 

                What I understood from your post is, Postman has limitations in regard of adding attachments (not possible). As I am not familiar with other coding tools yet (Postman seemed to be simple - probably to simple ; )  I am not able to implement your examples right away - but I will pass your inputs to the right person resp. the involved interface developer. Thx