REST API - Create Document - HTTPClient

I have a auth ticket, and have set a link / URI to ContentServer.

  • I can get nodes
  • I can create folders and categories
  • I don't know how to upload a document. See the code below.

What is needed to fix this?

//Create MultiPart Form Data
MultipartFormDataContent mainRequest = new MultipartFormDataContent();

//Setting the Post parameters
Dictionary<string, string> abc = new Dictionary<string, string>();
abc.Add("type", "144");
abc.Add("parent_id", parentId.ToString());
abc.Add("name", "testDocument");

FormUrlEncodedContent form = new FormUrlEncodedContent(abc);

//Setting the Document
byte[] bb = File.ReadAllBytes("c:\\temp\\testWord.docx");
ByteArrayContent file = new ByteArrayContent(bb);
file.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");


//Adding preps to FormData
mainRequest.Add(form);
mainRequest.Add(file, "file");


//Call
//500 internal error.
HttpResponseMessage response = _client.PostAsync("nodes", mainRequest).Result;
response.EnsureSuccessStatusCode();
Tagged:

Comments

  • Answered my own question, I was rather close. Hope I help someone else with this.

    2 changes:

    //Setting POST parameters, no dictionary but stringcontents
    StringContent type = new StringContent("144");
    StringContent parent = new StringContent(parentId.ToString());
    StringContent name = new StringContent("asdfasd.docx"); //name in content server
    
    //Add Parameters and Document
    mainRequest.Add(type, "\"type\"");
    mainRequest.Add(parent, "\"parent_id\"");
    mainRequest.Add(name, "\"name\"");
    mainRequest.Add(file, "file", "c:\\temp\\testWord.docx"); //this has also changed, add filename.
    
  • This was really helpful, thanks. Do you happen to have sample code for updating/adding Categories to a document?

  • Hi Paul or Kevin,

    Kindly provide me the basic apis on open text like authentication, add document, add folder and search documents using restful api. I am new to open text.

    Thanks in advance

    Regards,
    Sunil
  • Hi Paul or Kevin,

    Kindly provide me the basic apis on open text like authentication, add document, add folder and search documents using restful api. I am new to open text.

    Than
  • Hi Paul or Kevin,

    Kindly provide me the basic apis on open text like authentication, add document, add folder and search documents using restful api. I am new to open text.

    Thanks in advance
    Sunil
  • Hi Paul or Kevin,

    Kindly provide me the basic apis on open text like authentication, add document, add folder and search documents using restful api. I am new to open text.

    Thanks in advance
    Sunil