2 Replies Latest reply on Jun 7, 2018 12:42 PM by roberto.surdich

    REST API - Attachments

    roberto.surdich

      We are using Web Help Desk, version 12.5.0 (Build #12.5.0.1257), and we are experiencing a problem with attachments, since the service "/attachment/upload**" documented in http://www.solarwinds.com/documentation/WebHelpDesk/docs/WHD_API/Web%20Help%20Desk%20API%20Guide.html is not working properly.

       

      Looking at the forum, we found out that this is a known issue (seeUploading attachment to a ticket via the API ), but we didn't succeed in getting it done even using the workaround reported here (similar error).

       

      Step after step, we succeeded in overcoming the problem, finding out that some changes had to be made to the parameters of the "original" API:

      - in place of WebObjects session ID (wosid) we must use the sessionKey (previously obtained by invoking the proper service (/WebObjects/Helpdesk.woa/ra/Session?apiKey=XXXXX&username=YYYY);

      - in the multipart/form-data, the parameter must be set as "file" and not as "fileUpload" (as documented).

       

      So, the final question is:

      - What is the proper way to upload an attachment?

      - Is the documented API correct (so, what are we doing wrong?) or we can go on with our corrections (so, will the API be corrected in the new version?)

       

      Thanks in advance for your support.

        • Re: REST API - Attachments
          fluffy midnight

          Hi roberto.surdich,

           

          Almost all of the documented API from 12.2.0 works all the way up to 12.5.2, I can confirm that uploading attachments through the API should work on 12.5.0 as mine is working as intended on the latest release.

           

          • You mention trying a workaround from that thread, is that with Postman or cURL? (I'm led to believe that cURL won't work with some of these features, additionally it isn't worth the effort to break down and fix when it's a fault of the software/API that isn't working appropriately)
          • Have you had the API working with uploading attachments before?
          • Is this only to upload attachments to requests or does this include replies too?
          • Can you provide the code you've used in this scenario?
          • Operating System WHD is installed on?
          • Database type linked to WHD?

           

          Unfortunately I cannot provide a straight-forward tutorial or fix as I have some custom command-line tools and don't have enough time to run through and document how to achieve the above... However, I might be able to assist in diagnosing or restructuring what you have so it works. Let's see what you have and we'll go from there.

           

          -Midnight

            • Re: REST API - Attachments
              roberto.surdich

              Hello.

              Here below my answers to your questions:

               

              • You mention trying a workaround from that thread, is that with Postman or cURL? (I'm led to believe that cURL won't work with some of these features, additionally it isn't worth the effort to break down and fix when it's a fault of the software/API that isn't working appropriately)

               

              We've tried the workaround with Postman at first and by code soon after, in both case we wasn't able to upload the attachment to the ticket so we didn't tried this way anymore.

               

              • Have you had the API working with uploading attachments before?

               

              No, we had never tried to upload attachments to tickets through the API before

               

              • Is this only to upload attachments to requests or does this include replies too?

               

              I'm not sure of understanding your question, anyway the aim of this tests is to upload the attachments in a ticket and to receive a confirmation from the server

               

              • Can you provide the code you've used in this scenario?

               

              Sure.

              See below.

               

              • Operating System WHD is installed on?

               

              Windows 2012 R2

               

              • Database type linked to WHD?

               

              PostgreSQL

               

               

              Just ore more thing I didn't tell you last time: the "logout" operation (see code below again) fails, with HTTP ERROR CODE 401, so we're also worried about the possibility that the session is not being cleaned up correctly; are we doing something wrong  about that?

               

               

              Thanks in advance for your support.

               

              Regards,

              Roberto Surdich

               

               

               

              -------------------------------------------------------------------------------------------------------------------------------------------

              package *******;

               

               

              import java.io.BufferedReader;

              import java.io.File;

              import java.io.IOException;

              import java.io.InputStream;

              import java.io.InputStreamReader;

              import java.io.OutputStream;

              import java.io.OutputStreamWriter;

              import java.io.PrintWriter;

              import java.net.HttpURLConnection;

              import java.net.URL;

              import java.nio.file.Files;

              import java.security.cert.X509Certificate;

              import java.util.List;

               

               

              import javax.net.ssl.HostnameVerifier;

              import javax.net.ssl.HttpsURLConnection;

              import javax.net.ssl.SSLContext;

              import javax.net.ssl.SSLSession;

              import javax.net.ssl.TrustManager;

              import javax.net.ssl.X509TrustManager;

               

               

              import org.json.JSONObject;

               

               

              public class TestAggiuntaAllegato {

               

               

                  public static final String ADMIN = "?username=admin&password=**********";

               

               

                  public static void main(final String[] args) {

                      try {

                          // ~ CREAZIONE SESSIONE

                          URL url = new URL("https://********************/helpdesk/WebObjects/Helpdesk.woa/ra/Session" + ADMIN);

                          HttpURLConnection con = (HttpURLConnection) url.openConnection();

                          con.setRequestProperty("Accept", "application/json");

                          con.setRequestMethod("GET");

               

               

                          System.out.println("Risultato apertura sessione: " + con.getResponseCode());

               

               

                          JSONObject jsonObj = new JSONObject(getStringFromInputStream(con.getInputStream()));

                          String wosId = "wosid=" + jsonObj.get("sessionKey");

                          String jSessionId = getJSessionIdFromCookie(con.getHeaderFields().get("Set-Cookie"));

               

               

                          System.out.println(jSessionId);

                          System.out.println(wosId);

                          // ~ /CREAZIONE SESSIONE

               

               

                          String entityId = "2";

                          File textFile = new File("C:\\********************\\TEST.txt");

                          caricaFile(textFile, entityId, jSessionId, wosId);

               

               

                          // ~ LOGOUT

                          String logoutUrl = "https://********************/helpdesk/WebObjects/Helpdesk.woa/ra/Session?sessionKey=" + jsonObj.get("sessionKey");

                          URL urlLogout = new URL(logoutUrl);

                          HttpURLConnection logoutCon = (HttpURLConnection) urlLogout.openConnection();

                          logoutCon.setRequestMethod("DELETE");

                          System.out.println("Risultato chiusura sessione: " + logoutCon.getResponseCode());

                          // ~ /LOGOUT

                      } catch (Exception e) {

                          e.printStackTrace();

                      }

                  }

               

               

                  private static void caricaFile(final File file, final String entityId, final String jSessionId, final String wosId) throws Exception {

                      String CRLF = "\r\n";

                      String charset = "UTF-8";

                      String boundary = Long.toHexString(System.currentTimeMillis());

               

               

                      URL url = new URL("https://********************/helpdesk/attachment/upload?type=jobTicket&entityId=" + entityId + "&returnFields=id,uploadDate");

                      HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                      connection.setRequestMethod("POST");

                      connection.setDoOutput(true);

                      connection.setRequestProperty("Cookie", jSessionId + ";" + wosId);

                      connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

               

               

                      OutputStream output = connection.getOutputStream();

                      try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true)) {

                          writer.append("--" + boundary).append(CRLF);

                          writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"").append(CRLF);

                          writer.append(CRLF).flush();

                          Files.copy(file.toPath(), output);

                          output.flush();

                          writer.append(CRLF).append(CRLF).append("--" + boundary + "--").append(CRLF).flush();

                      }

               

               

                      System.out.println("Risultato caricamento file: " + connection.getResponseCode() + ", " + getStringFromInputStream(connection.getInputStream()));

                  }

               

               

                  private static String getJSessionIdFromCookie(final List<String> cookies) {

                      if (cookies != null && !cookies.isEmpty()) {

                          for (String cookie : cookies) {

                              if (cookie.startsWith("JSESSIONID") && cookie.contains(";")) {

                                  return cookie.split(";")[0];

                              }

                          }

                      }

                      return null;

                  }

               

               

                  public static String getStringFromInputStream(final InputStream is) {

                      if (is == null)

                          return "";

                      StringBuilder sb = new StringBuilder();

                      String line;

                      try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {

                          while ((line = br.readLine()) != null) {

                              sb.append(line);

                          }

                      } catch (IOException e) {

                          e.printStackTrace();

                      }

                      return sb.toString();

                  }

               

               

              }

               

               

               

              -------------------------------------------------------------------------------------------------------------------------------------------