Status: Open
Status: Answered
Status: Closed
Status: Duplicate

Uploading documents using the Java implementation of REST API not working

0
Posted Dec 18 by Mentor Belalla.
Updated Dec 18.

Greetings.

I'm new to the OPENTEXT Rest API and while I'm able to authenticate/create folders using it, I can't get the document upload to work. The following is the code I've been using:

import java.io.BufferedReader;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;


URIBuilder builder = new URIBuilder("https://bla.bla.com/otcs/cs.exe/api/v2/nodes");

builder.setParameter("type",                 "144")
       .setParameter("parent_id",            "123456")
       .setParameter("name",                 "bla.pdf")
       .setParameter("file",                 "C:\\My_Data\\bla.pdf");

MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();

multiPartBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multiPartBuilder.addBinaryBody("ufile", new File (fullFileName), ContentType.DEFAULT_BINARY, fileName);
multiPartBuilder.setBoundary("aall12dk@@Joey");

HttpPost httpPostRequest = new HttpPost(builder.build());

httpPostRequest.addHeader( "OTCSTICKET", "xfdfjadslfdurrreerwerwerwe" );
httpPostRequest.addHeader( HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=aall12dk@@Joey" );
httpPostRequest.addHeader( "Content-Disposition", "attachment;filename=" + "bla.pdf" );

httpPostRequest.setEntity(multiPartBuilder.build());

HttpResponse response = = httpClient.execute(httpPostRequest);

The errors I keep getting are:

00:47:47.694 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{"error":"Could not process object, invalid action \u0027create\u0027"}"

or if I remove the binary contents code:

00:21:15.458 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "{"error":"Error: File could not be found."}"

Unfortunately, the REST API is not nearly as well documented as the CWS/Soap based web services…in my opinion at least. I can't seem to find any sample code anywhere. Any help would be greatly appreciated.

Thank you

1 Answer

0
BEST ANSWER: As chosen by the author.

I got the solution for this. Basically, the answer is to use only a generic URIBuilder. Everything else goes to the multi-part builder:

        URIBuilder builder = new URIBuilder("https://bla.bla.com/otcs/cs.exe/api/v2/nodes");

        MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();

        multiPartBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multiPartBuilder.setBoundary("aall12dk@@WhateverYouWishBoundary2019");            //Random value basically.

        multiPartBuilder.addPart("type",                 new StringBody(String.valueOf(LAPI_DOCUMENTS.DOCUMENTSUBTYPE), ContentType.MULTIPART_FORM_DATA));
        multiPartBuilder.addPart("parent_id",            new StringBody(String.valueOf(parentId),                       ContentType.MULTIPART_FORM_DATA));
        multiPartBuilder.addPart("name",                 new StringBody(fileName,                                       ContentType.MULTIPART_FORM_DATA));
        multiPartBuilder.addPart("file",                 new FileBody(new File (fullFileName),                          ContentType.DEFAULT_BINARY));
        multiPartBuilder.addPart("description",          new StringBody(comments,                                       ContentType.MULTIPART_FORM_DATA));
        multiPartBuilder.addPart("external_create_date", new StringBody("2017-12-10",                                   ContentType.MULTIPART_FORM_DATA)); 
        multiPartBuilder.addPart("external_modify_date", new StringBody(LocalDate.now().toString(),                     ContentType.MULTIPART_FORM_DATA)); 
        multiPartBuilder.addPart("external_source",      new StringBody("ftp",                                          ContentType.MULTIPART_FORM_DATA));

        HttpPost httpPostRequest = new HttpPost(builder.build());

        httpPostRequest.addHeader("OTCSTICKET", sessionToken);
        httpPostRequest.setEntity(multiPartBuilder.build());

        HttpResponse response = httpClient.execute(httpPostRequest);       // you want to do a response = .... or you'll end up with hanging connections.

        if ( response.getStatusLine().getStatusCode() != 200 ) {
            // your error handling here
        }

 You have subscribed and will receive email notifications of updates to this topic. To unsubscribe, uncheck the checkbox.

Statistics

Related categories

Related tags

Your answer

To leave an answer, please sign in.