无法通过 Softlayer create ticket rest 附加文件 api

Unable to attach files through Softlayer create ticket rest api

我们可以通过 SoftLayer 创建没有附件的工单 API。我们在处理附件时遇到了同样的问题。我们可以使用 SLAPI 检索附件。 我们已经尝试使用 rest api 和基本 64 位编码、字节数组、utf-8 和 ascii 附加文件,通过 java。帮助我们。

这是创建带有附件的标准工单的休息请求:

https://$username:$apiKey@api.softlayer.com/rest/v3/SoftLayer_Ticket/createStandardTicket

方法:Post

{  
   "parameters":[  
      {  
         "assignedUserId":112233,
         "subjectId":1001
      },
      "This content is for test",
      0, "", "", "",
      [  
         {  
            "filename":"file.txt",
            "data":"test test RCV"
         }
      ]
   ]
}

用您自己的信息替换:$username、$apiKey 和 112233 值。

如果您使用的是 SoftLayer API Client for Java,目前似乎无法通过附件创建标准票证,我尝试了不同的方法,但未能成功上传文件。

不过,我可以提供一个解决方法来避免这个问题,请尝试以下脚本:

package Tickets;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.Ticket;
import com.softlayer.api.service.container.utility.file.Attachment;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * This script creates standard ticket and attach file
 *
 * Important Manual Page:
 * http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/createStandardTicket
 * http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/addAttachedFile
 * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket
 * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Utility_File_Attachment
 *
 * @license <http://sldn.softlayer.com/article/License>
 * @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
 * @version 0.2.2
 */
public class CreateStandardTicket {
    /**
     * This is the constructor, is used to create Standard Ticket, it shows basic
     * properties for creating a ticket.
     */
    public CreateStandardTicket() {
        // Declare your SoftLayer username and apiKey
        String username = "set me";
        String apiKey = "set me";
        // Declare the data of the ticket you wish to submit
        Long assignedUserId = new Long(112233);
        boolean notifyUserOnUpdateFlag = true;
        Long subjectId = new Long(1001);
        String title = "New Standard Ticket for Test";
        // Declare others parameters of the ticket
        String contents = "New content for test";
        Long attachmentId = null;
        String rootPassword = "";
        String controlPanelPassword = "";
        String accessPort = "";
        String attachmentType = "";

        // Declare the name of the file that will upload to the SoftLayer API and the path where the file is located
        String filename = "fileTest.txt";
        String path = "C:/Users/Test/Pictures/test.txt";

        // Get Api Client and SoftLayer_Ticket service
        ApiClient client = new RestApiClient().withCredentials(username, apiKey);
        Ticket.Service ticketService = Ticket.service(client);

        // Build SoftLayer_Ticket object containing ticket information
        Ticket newStandard = new Ticket();
        newStandard.setAssignedUserId(assignedUserId);
        newStandard.setNotifyUserOnUpdateFlag(notifyUserOnUpdateFlag);
        newStandard.setSubjectId(subjectId);
        newStandard.setTitle(title);
        // Build SoftLayer_Container_Utility_File_Attachment object
        Attachment newFile = new Attachment();
        List<Attachment> attachedFiles = new ArrayList<Attachment>();
        attachedFiles.add(newFile);

        try {
            // Creating standard ticket
            Ticket ticketCreated = ticketService.createStandardTicket(newStandard, contents, attachmentId, rootPassword, controlPanelPassword, accessPort, attachedFiles, attachmentType);
            ticketService = Ticket.service(client, new Long(ticketCreated.getId()));

            // Reading the file in bytes
            File file = new File(path);
            int length = (int) file.length();
            BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
            byte[] bytes = new byte[length];
            reader.read(bytes, 0, length);
            reader.close();

            // Build SoftLayer_Container_Utility_File_Attachment object
            newFile.setData(bytes);
            newFile.setFilename(filename);

            // Attaching the file to the ticket
            com.softlayer.api.service.ticket.attachment.File result = ticketService.addAttachedFile(newFile);
            System.out.println("The ticket was created successfully: " + ticketCreated.getId());

        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }

    /**
     * This is the main method which makes use of CreateStandardTicket method.
     *
     * @param args
     * @return Nothing
     */
    public static void main(String[] args) {
        new CreateStandardTicket();
    }


}

如您在脚本中所见,它首先创建工单,然后将文件上传到工单。

希望对你有帮助。