3

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

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

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&parent_id={folderId}&name={docName}";

4

3 回答 3

2

您的 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
于 2015-12-28T21:11:18.703 回答
1

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

使用文件 API 在 JavaScript 中读取文件

这是一个 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);
});
于 2016-09-27T23:08:02.357 回答
0

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

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

我发现这样做的唯一方法是将文件上传/发送到 Content Servers Web 服务器上的“上传”目录(在我们的服务器上,它设置为 D:\Upload)。此目录位置可在管理部分。

将文件发送到 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地址或主机名来上传这个庄园的文件。您必须输入要上传到的服务器的本地路径。但是,您可以只提供 Upload 目录中存在的文件名,OpenText API 似乎可以很好地找到它。

例如,您可以通过D:\Uploads\Document.txt Document.txt

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

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

于 2019-10-16T20:01:40.770 回答