2 Replies Latest reply on Jun 21, 2019 10:56 AM by goutam.holla

    Uploading Image from Local using rest api

    goutam.holla

      Hi Team,

      Matt CollingeJens GoldhammerJohn ReynoldsGopi Gorantala

       

      I am trying to upload image to Jive using image upload rest api and able to upload uaing "Postman"

      And trying to achieve same in JAVA and getting error code 400.Below is the code snippet.

       

      DefaultHttpClient httpClient = new DefaultHttpClient();

      HttpPost postRequest = null;

      String url="https://chubb-v9.hosted.jivesoftware.com/api/core/v3/images";

      postRequest = new HttpPost(url);

      postRequest.addHeader(ibHelper.getAuthenticationCode());

      postRequest.addHeader("content-type", "multipart/form-data");

      MultipartEntity multiPartEntity = new MultipartEntity () ;

      FileBody fileBody = new FileBody(file) ;//image file local path

      multiPartEntity.addPart("file",fileBody);

      multiPartEntity.addPart("filename", new StringBody("test"));

      postRequest.setEntity(multiPartEntity);

      headers = postRequest.getAllHeaders();

      response = httpClient.execute(postRequest);

       

       

      Please let me know how to resolve this issue.

      CC:Sowjanya Gonuguntla yojana SINGH

       

      Thanks,

      Goutam

        • Re: Uploading Image from Local using rest api
          gopi.gorantala

          Hey goutam holla,

           

          I have done similar stuff in a different way, its a spring-boot application.. my code creates different content types by grabbing random data(Subject, body, tags, categories) from a JSON, + attachments URL's. etc..

           

          Sample code, which is suffice for you to replicate the image creation functionality..

           

          //implementation package

          @Override
          public void createJiveDocument() {

            HttpEntity<String> request = new HttpEntity<String>(jiveDocBody(),createHeaders(userSyncConfig.getJiveUsername(), userSyncConfig.getJivePassword()));
             RestTemplate restTemplate = new RestTemplate();
             restTemplate.setErrorHandler(new CustomResponseErrorHandler());
             restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
             HttpEntity<String> response = restTemplate.exchange(userSyncConfig.getJiveBaseURL() + "api/core/v3/contents", HttpMethod.POST, request, String.class);
            if (s_log.isDebugEnabled())   s_log.debug("XXXXXXXXXXXXXXXX----------------- >>> JIVE DOCUMENT CREATED <<< -----------------XXXXXXXXXXXXXXXX");
          }

           

          //and below all are into custom util package

           

          public static HttpHeaders createHeaders(final String username, final String password) {

            HttpHeaders httpHeader = new HttpHeaders() {

            {

            String auth = username + ":" + password;
            byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
             String authHeader = "Basic " + new String(encodedAuth);
             set("Authorization", authHeader);
             }

            };
             httpHeader.setContentType(MediaType.APPLICATION_JSON);
            return httpHeader;
          }

           

          public class CustomResponseErrorHandler implements ResponseErrorHandler {

             private static final Logger s_log = Logger.getLogger(CustomResponseErrorHandler.class);

            private ResponseErrorHandler myErrorHandler = new DefaultResponseErrorHandler();

            public boolean hasError(ClientHttpResponse response) throws IOException {

             return myErrorHandler.hasError(response);
             }

           

             public void handleError(ClientHttpResponse response) throws IOException {

            String body = org.apache.commons.io.IOUtils.toString(response.getBody()),
             responseCode = response.getStatusCode().toString();

            switch (responseCode) {

             case "500":

           

            break;
            case "400":

             s_log.error("payload incorrect..");
            break;

            default:

             throw new RuntimeException(body);
             }

            }

          }

           

          private String jiveDocBody() {

             return "{" +

             "\"content\":" +

             "{\"type\": \"text/html\"," +

             "\"text\": \"<body><p>" +

             "</p></body>\"}," +

             "\"subject\": \"Jive importer - Execution results: " + LocalDateTime.now() + "\"," +

          // "\"parent\": \"JIVEURL/api/core/v3/places/12345\"," +
             "\"type\": \"document\"" +

             "}";
             }

           

           

          so if you see here, jiveDocBody method has the payload defined in a String format.

          1 person found this helpful