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?
How to upload document via API?
Re: How to upload document via API?
I only have code in C# so see if you can work from that:
For the parameters:
url (string) = "http://mayan.server.com/api/documents/";
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.
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);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
bool sucess = true;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
}
catch
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
sucess = false;
}
finally
{
wr = null;
}
return sucess;
url (string) = "http://mayan.server.com/api/documents/";
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.