How to upload document via API?

Anything concerning integration via the REST API
Post Reply
Posts: 1
Joined: Mon Apr 27, 2020 7:41 am

How to upload document via API?

Post by whitenexx »

I already tested a lot of request combinations but couldn't successfully upload a document to mayan via api.
Could someone please post a valid curl request?
Posts: 5
Joined: Tue Dec 10, 2019 4:36 pm

Re: How to upload document via API?

Post by mikeg »

I only have code in C# so see if you can work from that:

Code: Select all

        public static bool HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc, AuthInfo cred)
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(cred.UserName + ":" + cred.Password));
            wr.Headers.Add("Authorization", "Basic " + encoded);

            Stream rs = wr.GetRequestStream();

            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
            foreach (string key in nvc.Keys)
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header = string.Format(headerTemplate, paramName, file, contentType);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);

            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                rs.Write(buffer, 0, bytesRead);

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);

            WebResponse wresp = null;
            bool sucess = true;
                wresp = wr.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                if (wresp != null)
                    wresp = null;
                sucess = false;
                wr = null;
            return sucess;
For the parameters:
url (string) = "";
nvc (NameValueCollection) = {
"description" => "",
"document_type" => "1", (this will be your document_type_id value - adjust as needed)
"label" => "filename.ext",
"langauge" => "eng"
paramname (string) = "file"
file (string) = "c:\filestoupload\example.pdf" (path to file you're uploading - adjust as needed)
contentType (string) = "application/pdf" (mimetype - adjust as needed)
cred (AuthInfO) - this is a custom class but its basically just the username and password within Mayan for the current user - since I use LDAP authentication for both Mayan and the app that uses this class, then I have their credentials saved into memory and can pass them to Mayan whenever they are doing things like adding documents.

Note: for the label I initially pass in a random GUID so that I can then query the database for that label to get back the document ID and I can then upload/set metadata on it, then I'll change the label to something more human-readable afterwards.
Post Reply