无法通过Softlayer创建票证休息api来附加文件

时间:2016-04-14 08:57:19

标签: ibm-cloud-infrastructure

我们可以通过SoftLayer API创建没有附件的票证。我们在附件方面也有同样的问题。我们可以使用SLAPI检索附件。 我们尝试使用带有基本64位编码的rest api,字节数组,utf-8和ascii,通过java附加文件。帮助我们。

1 个答案:

答案 0 :(得分:2)

这是使用附加文件创建标准票证的休息请求:

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

方法:发布

{  
   "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();
    }


}

正如您在脚本中看到的那样,首先创建故障单,然后将文件上传到故障单。

我希望它对你有所帮助。

相关问题