简体   繁体   中英

How can I add a new document to Content Server 10.5 using the REST api

How can I add a new document to Content Server 10.5 using the REST api?

I am following the Swagger docs for creating a node, but it is not clear how I attach the file to the request. Here is (roughly) the code I am using:

var folderId = 2000;
var docName = "test";

var uri = $"http://[serverName]/otcs/llisapi.dll/api/v1/nodes?type=144&parent_id={folderId}&name={docName}";    

var request = new HttpRequestMessage();
request.Headers.Add("Connection", new[] { "Keep-Alive" });
request.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); 
request.Headers.Add("Pragma", "no-cache");     
request.Headers.Add("OTCSTicket", /* ticket here */);
request.RequestUri = new Uri(uri);
request.Method = HttpMethod.Post;
request.Content = new ByteArrayContent(data);
request.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filePath));
request.Headers.ExpectContinue = false;

var httpClientHandler = new HttpClientHandler
{
  Proxy = WebRequest.GetSystemWebProxy(),
  UseProxy = true,
  AllowAutoRedirect = true
};

using (var client = new HttpClient(httpClientHandler))
{
  var response = client.SendAsync(request).Result;
  IEnumerable<string> temp;
  var vals = response.Headers.TryGetValues("OTCSTicket", out temp) ? temp : new List<string>();
  if (vals.Any())
  {
    this.ticket = vals.First();
  }

  return response.Content.ReadAsStringAsync().Result;
}

I've been searching through the developer.opentext.com forums, but finding a complete example in c# is proving tough - there are a few examples in javascript, but attempting to replicate these in c# or via chrome or firefox extensions just give the same results. Calling other CS REST methods has not been an issue so far, this is the first one that's giving me problems.

Edit: I pasted the wrong url into my question, which I've now fixed. It was var uri = $"http://[serverName]/otcs/llisapi.dll/api/v1/forms/nodes/create?type=0&parent_id={folderId}&name={docName}"; .

Your URL doesn't look like the REST API, it's rather the traditional URL used for the UI.

This article should describe how to do what you want to do:

https://developer.opentext.com/webaccess/#url=%2Fawd%2Fresources%2Farticles%2F6102%2Fcontent%2Bserver%2Brest%2Bapi%2B%2Bquick%2Bstart%2Bguide&tab=501

EDITED:

Ok, so that's how it should work:

send a POST to http://www.your_content_server.com/cs[.exe]/api/v1/nodes

send this in your payload to create a document in your enterprise workspace

type=144
parent_id=2000
name=document_name.txt
<file>

A incomplete demo in Python would look like this. Make sure you get a valid ticket first.

files = {'file': (open("file.txt", 'rb')}
data = { 'type': 144, 'parent_id': 2000, 'name': 'document_name.txt' }
cs = requests.post(url, headers={'otcsticket':'xxxxxxx'}, data=data, files=files)
if cs.status_code == 200:
    print "ok"
else:
    print cs.text

You will need a form input to get the file onto the page then you can use filestreams to redirect it, there is great guide for that here.

Reading files in JavaScript using the File APIs

Here is a Jquery/ Ajax example.

I find the best way to go about this is to use Postman (Chrome Plugin) to experiment until you get comfortable.

var form = new FormData();
form.append("file", "*filestream*"); 
form.append("parent_id", "100000");
form.append("name", "NameYourCreatedFile");
form.append("type", "144");

var settings = {
  "async": true,
  "url": "/cs.exe/api/v1/nodes", // You will need to amend this to match your environment
  "method": "POST",
  "headers": {
    "authorization": "Basic **use Postman to generate this**",
    "cache-control": "no-cache",
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM