【问题标题】:How can I add a new document to Content Server 10.5 using the REST api如何使用 REST api 将新文档添加到 Content Server 10.5
【发布时间】:2015-12-28 09:53:17
【问题描述】:

如何使用 REST api 向 Content Server 10.5 添加新文档?

我正在关注Swagger docs 创建节点,但不清楚我如何将文件附加到请求中。这是(大致)我正在使用的代码:

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;
}

我一直在搜索developer.opentext.com 论坛,但在 c# 中找到一个完整的示例证明是困难的 - 在 javascript 中有一些示例,但尝试在 c# 中或通过 chrome 或 firefox 扩展复制这些示例只需给出相同的结果。到目前为止,调用其他 CS REST 方法还不是问题,这是第一个给我带来问题的方法。

编辑:我将错误的网址粘贴到我的问题中,现在我已经修复了。是var uri = $"http://[serverName]/otcs/llisapi.dll/api/v1/forms/nodes/create?type=0&amp;parent_id={folderId}&amp;name={docName}";

【问题讨论】:

  • 我根据 Steffen 的问题更新了网址,但仍然收到 400 个错误请求错误。有没有我可以检查的日志,看看可能出了什么问题。我还看到了一些关于 CS UI 小部件的信息 - 它们在哪里以及它们有什么帮助?
  • 我只是在重新阅读您的问题。我注意到您已将参数打包到 url 中。但是你想执行 POST,因此所有这些参数都必须进入正文。

标签: c# rest opentext livelink


【解决方案1】:

您的 URL 看起来不像 REST API,而是用于 UI 的传统 URL。

这篇文章应该描述如何做你想做的事:

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

已编辑:

好的,这就是它应该如何工作的:

发送 POST 到 http://www.your_content_server.com/cs[.exe]/api/v1/nodes

在您的有效负载中发送它以在您的企业工作区中创建一个文档

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

不完整的 Python 演示如下所示。请确保您首先获得有效票证。

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

【讨论】:

  • 这就是我发现的令人沮丧的地方 - swagger 文档和快速入门指南似乎相互矛盾。 swagger 文档声明类型、父 ID 和名称应该在查询字符串上,但快速启动状态应该在请求正文中。他们都没有提到如何附加要上传的实际文件。理想的是一个有效的 c# 或 javascript 示例。我仍然收到 400 个错误,但它们不足以为我指明正确的方向。
  • 如何在请求中添加BuilingId .tried 下面的data = { 'type': 144, 'parent_id': 2000, 'name': 'document_name.txt' ,'BuilingId ': 'xxxx'}显示以下错误“BuilingId 是必填字段。”
【解决方案2】:

你需要一个表单输入来将文件放到页面上,然后你可以使用文件流来重定向它,这里有很好的指南。

Reading files in JavaScript using the File APIs

这是一个 Jquery/Ajax 示例。

我发现解决此问题的最佳方法是使用 Postman(Chrome 插件)进行试验,直到您感到舒服为止。

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);
});

【讨论】:

    【解决方案3】:

    OpenText API 似乎只支持通过异步 JavaScript 上传的文件上传 - 不支持通过使用包含文件内容的典型发布表单请求的传统文件上传(老实说这非常糟糕 - 因为这将是最容易在服务器端处理)。

    我已经联系了他们的支持,但他们完全没有帮助 - 他们说因为它使用 JavaScript,所以他们无法帮助我。使用 JavaScript 以外的任何语言的其他任何人都是 SOL。我提交了我的整个 API 包,但他们没有费心调查并想尽快关闭我的票。

    我发现的唯一方法是将文件上传/发送到您的内容服务器网络服务器上的“上传”目录(在我们的服务器上,它设置为 D:\上传)。此目录位置可在管理部分配置。

    将文件发送到 Web 服务器后,发送一个创建节点请求,并将 file 参数设置为驻留在服务器上的文件的完整文件路径,如 OpenText API 将尝试从此目录中检索文件。

    我为此创建了一个 PHP API,您可以在这里浏览它的用法:

    https://github.com/FBCLIT/OpenTextApi

    <?php
    
    use Fbcl\OpenTextApi\Client;
    
    $client = new Client('http://server.com/otcs/cs.exe', 'v1');
    
    $api = $client->connect('username', 'secret');
    
    try {
        // The folder node ID of where the file will be created under.
        $parentNodeId = '12356';
    
        // The file name to display in OpenText
        $fileName = 'My Document.txt';
    
        // The actual file path of the file on the OpenText server.
        $serverFilePath = 'D:\Upload\My Document.txt';
    
        $response = $api->createNodeDocument($parentNodeId, $fileName, $serverFilePath);
    
        if (isset($response['id'])) {
            // The ID of the newly created document will be returned.
            echo $response['id']; 
        }   
    } catch (\Exception $ex) {
        // File not found on server drive, or issue creating node from given parent.
    }
    

    MIME 类型检测似乎是自动发生的,您无需发送任何内容即可检测文件类型。您可以将文件命名为您喜欢的任何名称,无需扩展名。

    我还发现您不能使用 IP 地址或主机名在此庄园上传文件。您必须输入要上传到的服务器的本地路径。但是,您可以只提供上传目录中存在的文件名,OpenText API 似乎可以很好地找到它。

    例如,您可以传递D:\Uploads\Document.txt Document.txt

    如果你没有正确完成,你应该得到错误:

    客户端错误:POST http://server.com/otcs/cs.exe/api/v1/nodes 导致 400 Bad Request 响应:{"error":"错误:在上传目录中找不到文件。"}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      相关资源
      最近更新 更多